emacs-orgmode@gnu.org archives
 help / color / mirror / code / Atom feed
* How to change working directory in ob-matlab source block?
@ 2023-04-13  4:57 stardiviner
  0 siblings, 0 replies; 3+ messages in thread
From: stardiviner @ 2023-04-13  4:57 UTC (permalink / raw)
  To: Org-mode

[-- Attachment #1: Type: text/plain, Size: 243 bytes --]

[stardiviner]           <Hack this world!>      GPG key ID: 47C32433
IRC(freeenode): stardiviner                     Twitter:  @numbchild
Key fingerprint = 9BAA 92BC CDDD B9EF 3B36  CB99 B8C4 B8E5 47C3 2433
Blog: http://stardiviner.github.io/

[-- Attachment #2: Type: text/html, Size: 696 bytes --]

^ permalink raw reply	[flat|nested] 3+ messages in thread

* How to change working directory in ob-matlab source block?
@ 2023-04-13  5:01 stardiviner
  2023-04-13  5:35 ` [SOLVED] " Christopher M. Miles
  0 siblings, 1 reply; 3+ messages in thread
From: stardiviner @ 2023-04-13  5:01 UTC (permalink / raw)
  To: Org-mode

[-- Attachment #1: Type: text/plain, Size: 2172 bytes --]

Is there friend know Emacs Org mode and Matlab here?

I try to save Matlab plot into image file in another path instead of
current working directory. Like
bellowing code:

#+headers: :var cwd=(expand-file-name "data/images/" (file-name-directory
(buffer-file-name)))
#+headers: :prologue "eval(sprintf("cd '%s'", cwd))"
#+headers: :epilogue "saveas(gcf,'matlab-plot-demo2.png'); close(gcf);"
#+begin_src matlab :results graphics file link :dir "data/images" :file
"matlab-plot-demo2.png" :session "*MATLAB*"
bar([1 11 7 8 2 2 9 3 6])
#+end_src

#+RESULTS[(2023-04-13 00:09:01) c5fc0d36b27e75610664ee69ae618649637e45d9]:
[[file:data/images/matlab-plot-demo2.png]]

But Matlab command "cd" does not work correctly in upper code method.

Currently I come up with two solutions:

solution 1

#+begin_src matlab :results graphics file link :file
"data/images/matlab-plot-demo2.png" :session "*MATLAB*" :var
img="data/images/matlab-plot-demo2.png"
bar([1 11 7 8 2 2 9 3 6])
% saveas(gcf, 'data/images/matlab-plot-demo2.png')
% saveas(gcf, img)
disp(img)
close(gcf)
#+end_src

#+RESULTS[(2023-04-13 12:08:35) b87e1d8687e33acc089ea7374829c4d41e3dd0e4]:
[[file:data/images/matlab-plot-demo2.png]]

solution 2

#+headers: :var cwd=(expand-file-name "data/images/" (file-name-directory
(buffer-file-name)))
#+headers: :prologue "eval(sprintf("cd '%s'", cwd))"
#+headers: :epilogue "saveas(gcf,'matlab-plot-demo2.png'); close(gcf);"
#+begin_src matlab :results graphics file link :dir "data/images" :file
"matlab-plot-demo2.png" :session "*MATLAB*"
bar([1 11 7 8 2 2 9 3 6])
#+end_src

#+RESULTS[(2023-04-13 00:09:01) c5fc0d36b27e75610664ee69ae618649637e45d9]:
[[file:data/images/matlab-plot-demo2.png]]

Both does not work.

If you got a workable solution, please help me, Thanks very much.

[stardiviner]           <Hack this world!>      GPG key ID:
47C32433IRC(<span zeum4c4="PR_2_0" data-ddnwab="PR_2_0"
aria-invalid="spelling" class="LI ng">freeenode</span>): stardiviner
                    Twitter:  @numbchildKey fingerprint = 9BAA 92BC CDDD
B9EF 3B36  CB99 B8C4 B8E5 47C3 2433Blog: <a href="
http://stardiviner.github.io/" target="_blank">http://stardiviner.github.io/
</a>

[-- Attachment #2: Type: text/html, Size: 3039 bytes --]

^ permalink raw reply	[flat|nested] 3+ messages in thread

* [SOLVED] Re: How to change working directory in ob-matlab source block?
  2023-04-13  5:01 How to change working directory in ob-matlab source block? stardiviner
@ 2023-04-13  5:35 ` Christopher M. Miles
  0 siblings, 0 replies; 3+ messages in thread
From: Christopher M. Miles @ 2023-04-13  5:35 UTC (permalink / raw)
  To: Org-mode

[-- Attachment #1: Type: text/plain, Size: 2128 bytes --]


stardiviner <numbchild@gmail.com> writes:

> Is there friend know Emacs Org mode and Matlab here?
>
> I try to save Matlab plot into image file in another path instead of current working directory. Like
> bellowing code:
>
> #+headers: :var cwd=(expand-file-name "data/images/" (file-name-directory (buffer-file-name)))
> #+headers: :prologue "eval(sprintf("cd '%s'", cwd))"
> #+headers: :epilogue "saveas(gcf,'matlab-plot-demo2.png'); close(gcf);"
> #+begin_src matlab :results graphics file link :dir "data/images" :file "matlab-plot-demo2.png" :session
> "*MATLAB*"
> bar([1 11 7 8 2 2 9 3 6])
> #+end_src
>
> #+RESULTS[(2023-04-13 00:09:01) c5fc0d36b27e75610664ee69ae618649637e45d9]:
> [[file:data/images/matlab-plot-demo2.png]]
>
> But Matlab command "cd" does not work correctly in upper code method.
>

I find out the solution. The "cd" command used as a function then pass
in path string as argument. It works fine. So I adopt source block like
this:

#+headers: :var cwd=(expand-file-name "data/images/" (file-name-directory (buffer-file-name)))
#+headers: :prologue "cd(cwd)"
#+begin_src matlab :session "*MATLAB*" :results output
% eval(sprintf("cd '%s'", cwd))
% eval(sprintf("cd('%s')", cwd))
% cd(cwd)

% returns the path to the current working directory.
pwd
#+end_src

#+RESULTS[(2023-04-13 13:20:27) 0c1259b084fc93f95d7c60c9c748a0630bf4391d]:
#+begin_example
cd(cwd)
cwd='/Users/stardiviner/Org/Wiki/Computer Technology/Programming/Emacs/Emacs Packages/Org mode/data/images/';
% eval(sprintf("cd '%s'", cwd))
% eval(sprintf("cd('%s')", cwd))
% cd(cwd)
% returns the path to the current working directory.
pwd

ans =

    '/Users/stardiviner/Org/Wiki/Computer Technology/Programming/Emacs/Emacs Packages/Org mode/data/images'
#+end_example

Really tricky for me. Hope helpful for some user.

-- 

[ stardiviner ]
I try to make every word tell the meaning that I want to express without misunderstanding.

Blog: https://stardiviner.github.io/
IRC(libera.chat, freenode): stardiviner, Matrix: stardiviner
GPG: F09F650D7D674819892591401B5DF1C95AE89AC3

[-- Attachment #2: signature.asc --]
[-- Type: application/pgp-signature, Size: 487 bytes --]

^ permalink raw reply	[flat|nested] 3+ messages in thread

end of thread, other threads:[~2023-04-13  5:38 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2023-04-13  5:01 How to change working directory in ob-matlab source block? stardiviner
2023-04-13  5:35 ` [SOLVED] " Christopher M. Miles
  -- strict thread matches above, loose matches on Subject: below --
2023-04-13  4:57 stardiviner

Code repositories for project(s) associated with this public inbox

	https://git.savannah.gnu.org/cgit/emacs/org-mode.git

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).