Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>You're asking two questions. I'll answer both, including why the second one might be impossible depending on your operating system (and definitely impossible on mine), then offer a suggestion on a problem dependent workaround.</p> <p>First, I use a script like this when getting Matlab to interact with a shell.</p> <pre><code>#!/bin/sh cat &lt;&lt;EOF | matlab -nodesktop -nosplash -nodisplay A=matlab_test('$1','$2'); system(['export temp1=' A]); %doesn't work setenv('temp2',A); %also doesn't work, I'll explain why below exit EOF echo $temp1 echo $temp2 </code></pre> <p>gives output:</p> <pre><code>[XXXXXX@compute-0-138 ~]$ ./stack_ex test matlab Warning: No window system found. Java option 'MWT' ignored &lt; M A T L A B (R) &gt; Copyright 1984-2010 The MathWorks, Inc. Version 7.12.0.635 (R2011a) 64-bit (glnxa64) March 18, 2011 To get started, type one of these: helpwin, helpdesk, or demo. For product information, visit www.mathworks.com. &gt;&gt; param1 : test param2 : matlab &gt;&gt; &gt;&gt; &gt;&gt; </code></pre> <p>So clearly the two versions of setting environment variables doesn't work. This leads us to your second question.</p> <p>The reason behind the failure to 'echo' is that both <code>system</code> and <code>setenv</code> create shells that are closed when Matlab is closed. That is to say, Matlab cannot set environment variables outside the shell that called it.</p> <p>There's a workaround for this for Windows systems discussed <a href="http://www.mathworks.com/matlabcentral/newsreader/view_thread/300759" rel="noreferrer">in this posting</a>, that uses a tool from Microsoft. Also mentioned <a href="http://www.mathworks.com/support/solutions/en/data/1-3IOL0N/index.html?product=SL&amp;solution=1-3IOL0N" rel="noreferrer">here</a>.</p> <p>I don't believe there's a workaround for *nix systems to set environment variables from within Matlab.</p> <p>Here's a method to do something similar to what you described.</p> <p>I'm assuming the use of echo is not what you actually want to do. Rather, I'm guessing that you'd like to use the string output stored in the environment variable to be used in further work with commands or scripts in the shell. One possible workaround would be the following:</p> <pre><code>#!/bin/sh cat &lt;&lt;EOF | matlab -nodesktop -nosplash -nodisplay A=matlab_test('$1','$2'); setenv('temp1',A); %doesn't work [a b] = system(['echo ' '$' 'temp1']) exit EOF </code></pre> <p>Giving output: [XXXXXX@compute-0-138 ~]$ ./stack_ex_3 test matlab Warning: No window system found. Java option 'MWT' ignored</p> <pre><code> &lt; M A T L A B (R) &gt; Copyright 1984-2010 The MathWorks, Inc. Version 7.12.0.635 (R2011a) 64-bit (glnxa64) March 18, 2011 To get started, type one of these: helpwin, helpdesk, or demo. For product information, visit www.mathworks.com. &gt;&gt; param1 : test param2 : matlab &gt;&gt; &gt;&gt; a = 0 b = hello matlab </code></pre> <p>This shows that the command <code>echo $temp1</code> was evaluated in the subshell created by <code>setenv</code> and temp1 holds the value assigned to it. The result of this call to the shell is now stored in b (a holds 0 indicating success). It is conceivable that you could pass the whole of what you would like to do in the shell through the system command, so that it runs in the subshell. We would have to know more specifics of your problem to give a complete assessment of this approach though.</p> <p>Edits and followup **********************</p> <p>The closest thing to wrapping Matlab and bash that I can think of is the following trick. You can pipe the output from the Matlab script to <code>myresult.out</code> with the following:</p> <pre><code>#!/bin/sh cat &lt;&lt;EOF | matlab -nodesktop -nosplash -nodisplay /&gt; myresult.out A=matlab_test('$1','$2'); disp(['grepMe ' A]) exit EOF </code></pre> <p>You can grep the <code>grepMe</code> line from <code>myresult.out</code>, pipe to sed, and select only the part of the output line you need, then pipe that on the the rest of your script. That's as close as I think you can get to what you're trying to do.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload