<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0">
  <channel>
    <link>http://www.mathworks.com/matlabcentral/newsreader/view_thread/251927</link>
    <title>MATLAB Central Newsreader - extract variable from base workspace</title>
    <description>Feed for thread: extract variable from base workspace</description>
    <language>en-us</language>
    <copyright>&amp;copy;1994-2012 by MathWorks, Inc.</copyright>
    <webmaster>webmaster@mathworks.com</webmaster>
    <generator>MATLAB Central Newsreader</generator>
    <docs>http://blogs.law.harvard.edu/tech/rss</docs>
    <ttl>60</ttl>
    <image>
      <title>MathWorks</title>
      <url>http://www.mathworks.com/images/membrane_icon.gif</url>
    </image>
    <item>
      <pubDate>Thu, 21 May 2009 22:41:02 -0400</pubDate>
      <title>extract variable from base workspace</title>
      <link>http://www.mathworks.com/matlabcentral/newsreader/view_thread/251927#651555</link>
      <author>Alan B</author>
      <description>I know I can do&lt;br&gt;
v = evalin('base','var')&lt;br&gt;
to store the value of base workspace variable var into function workspace variable v.&lt;br&gt;
&lt;br&gt;
Is there a way to check and be sure that var exists before attempting to assign it? I want this:&lt;br&gt;
v = evalin('base','if exist(var), var, else, defaultVar, end')&lt;br&gt;
but I can't use if/else/end inside the eval. I know I can do this with multiple evals, or try/catch, but is there a way to do it with one eval line?</description>
    </item>
    <item>
      <pubDate>Fri, 22 May 2009 02:21:01 -0400</pubDate>
      <title>Re: extract variable from base workspace</title>
      <link>http://www.mathworks.com/matlabcentral/newsreader/view_thread/251927#651569</link>
      <author>Zhelyazko </author>
      <description>This should work;&lt;br&gt;
function f&lt;br&gt;
evalin('base','if exist(''var'',''var'');temp=var; else; temp=defaultVar; end;assignin(''caller'',''v'',temp)')&lt;br&gt;
display(v);</description>
    </item>
    <item>
      <pubDate>Fri, 22 May 2009 14:51:01 -0400</pubDate>
      <title>Re: extract variable from base workspace</title>
      <link>http://www.mathworks.com/matlabcentral/newsreader/view_thread/251927#651677</link>
      <author>Alan B</author>
      <description>&quot;Zhelyazko &quot; &amp;lt;ztumbev@yahoo.com&amp;gt; wrote in message &amp;lt;gv526d$m9l$1@fred.mathworks.com&amp;gt;...&lt;br&gt;
&amp;gt; This should work;&lt;br&gt;
&amp;gt; function f&lt;br&gt;
&amp;gt; evalin('base','if exist(''var'',''var'');temp=var; else; temp=defaultVar; end;assignin(''caller'',''v'',temp)')&lt;br&gt;
&amp;gt; display(v);&lt;br&gt;
&lt;br&gt;
Thanks, that does work. I am confused though, what exactly are the rules on using if/else/end within an eval? I can do&lt;br&gt;
&lt;br&gt;
somevarLocal=evalin('base','somevar');&lt;br&gt;
&lt;br&gt;
but not&lt;br&gt;
&lt;br&gt;
somevarLocal=evalin('base','if exist(''var'',''var'');temp=var; else; temp=defaultVar; end; temp')&lt;br&gt;
&lt;br&gt;
but then your solution does work. This seems contradictory.</description>
    </item>
    <item>
      <pubDate>Fri, 22 May 2009 20:40:19 -0400</pubDate>
      <title>Re: extract variable from base workspace</title>
      <link>http://www.mathworks.com/matlabcentral/newsreader/view_thread/251927#651772</link>
      <author>Zhelyazko </author>
      <description>Actually this should work too.&lt;br&gt;
v=evalin('base','eval(''var'',''defaultVar'')');&lt;br&gt;
If you have an output you cannot use if and switch statements</description>
    </item>
    <item>
      <pubDate>Wed, 27 May 2009 16:39:02 -0400</pubDate>
      <title>Re: extract variable from base workspace</title>
      <link>http://www.mathworks.com/matlabcentral/newsreader/view_thread/251927#652816</link>
      <author>Alan B</author>
      <description>&quot;Zhelyazko &quot; &amp;lt;ztumbev@yahoo.com&amp;gt; wrote in message &amp;lt;gv72jj$9a8$1@fred.mathworks.com&amp;gt;...&lt;br&gt;
&amp;gt; Actually this should work too.&lt;br&gt;
&amp;gt; v=evalin('base','eval(''var'',''defaultVar'')');&lt;br&gt;
&amp;gt; If you have an output you cannot use if and switch statements&lt;br&gt;
&lt;br&gt;
Thanks Zhelyazko, that led me to my solution.&lt;br&gt;
&lt;br&gt;
For others who find this in the future: &lt;br&gt;
Both eval and evalin seem to have an (undocumented? I'm using 2006b) extra input argument, which is an eval string to be executed if an error is encountered in the first eval string (including 'undefined variable'). I can use this to do what I was asking with&lt;br&gt;
&lt;br&gt;
somevarlocal=evalin('base','somevar','defaultvalue');&lt;br&gt;
&lt;br&gt;
Is there more information on valid forms for eval strings somewhere? The documentation doesn't say much. Specifically, it says nothing about not allowing if/switch statements. What else is not allowed, and when?</description>
    </item>
    <item>
      <pubDate>Sat, 30 May 2009 00:41:02 -0400</pubDate>
      <title>Re: extract variable from base workspace</title>
      <link>http://www.mathworks.com/matlabcentral/newsreader/view_thread/251927#653523</link>
      <author>Zhelyazko </author>
      <description>More info &lt;br&gt;
&lt;a href=&quot;http://www.mathworks.com/support/tech-notes/1100/1103.html&quot;&gt;http://www.mathworks.com/support/tech-notes/1100/1103.html&lt;/a&gt;</description>
    </item>
  </channel>
</rss>

