Thread Subject: extract variable from base workspace

Subject: extract variable from base workspace

From: Alan B

Date: 21 May, 2009 22:41:02

Message: 1 of 6

I know I can do
v = evalin('base','var')
to store the value of base workspace variable var into function workspace variable v.

Is there a way to check and be sure that var exists before attempting to assign it? I want this:
v = evalin('base','if exist(var), var, else, defaultVar, end')
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?

Subject: extract variable from base workspace

From: Zhelyazko

Date: 22 May, 2009 02:21:01

Message: 2 of 6

This should work;
function f
evalin('base','if exist(''var'',''var'');temp=var; else; temp=defaultVar; end;assignin(''caller'',''v'',temp)')
display(v);

Subject: extract variable from base workspace

From: Alan B

Date: 22 May, 2009 14:51:01

Message: 3 of 6

"Zhelyazko " <ztumbev@yahoo.com> wrote in message <gv526d$m9l$1@fred.mathworks.com>...
> This should work;
> function f
> evalin('base','if exist(''var'',''var'');temp=var; else; temp=defaultVar; end;assignin(''caller'',''v'',temp)')
> display(v);

Thanks, that does work. I am confused though, what exactly are the rules on using if/else/end within an eval? I can do

somevarLocal=evalin('base','somevar');

but not

somevarLocal=evalin('base','if exist(''var'',''var'');temp=var; else; temp=defaultVar; end; temp')

but then your solution does work. This seems contradictory.

Subject: extract variable from base workspace

From: Zhelyazko

Date: 22 May, 2009 20:40:19

Message: 4 of 6

Actually this should work too.
v=evalin('base','eval(''var'',''defaultVar'')');
If you have an output you cannot use if and switch statements

Subject: extract variable from base workspace

From: Alan B

Date: 27 May, 2009 16:39:02

Message: 5 of 6

"Zhelyazko " <ztumbev@yahoo.com> wrote in message <gv72jj$9a8$1@fred.mathworks.com>...
> Actually this should work too.
> v=evalin('base','eval(''var'',''defaultVar'')');
> If you have an output you cannot use if and switch statements

Thanks Zhelyazko, that led me to my solution.

For others who find this in the future:
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

somevarlocal=evalin('base','somevar','defaultvalue');

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?

Subject: extract variable from base workspace

From: Zhelyazko

Date: 30 May, 2009 00:41:02

Message: 6 of 6

More info
http://www.mathworks.com/support/tech-notes/1100/1103.html

Tags for this Thread

Everyone's Tags:

Add a New Tag:

Separated by commas
Ex.: root locus, bode

What are tags?

A tag is like a keyword or category label associated with each thread. Tags make it easier for you to find threads of interest.

Anyone can tag a thread. Tags are public and visible to everyone.

Tag Activity for This Thread
Tag Applied By Date/Time
eval Alan B 21 May, 2009 18:44:04
rssFeed for this Thread

Contact us at files@mathworks.com