Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Access variable from other workspace in Matlab?

Is there something similar to assignin() to fetch a variable which exists in another workspace and get it to the local workspace, e.g. like accessing a base-workspace variable in a function!?

One example I'm currently trying to achieve is: I have a function bla(x) which takes one parameter. When x isn't being specified, the function should automatically use the x which exists in the base matlab workspace (if there is a x).

Something nice would be

function bla(x)
  if(nargin == 0 && exist('x', 'base', 'var'))
    x = fetchin('base', 'x');
  end

  % ...
end

I know that fetchin() doesn't exist and that exist() doesn#t take such a second parameter to check in a certain workspace! Thanks a lot in advance!

like image 927
tim Avatar asked Dec 20 '11 07:12

tim


1 Answers

The Matlab function evalin can do that:

x=evalin('base','x');

EDIT: As mentionned by Col Heather, you could use a try / catch statement to handle the errors that could be generated by the function evalin (in your case if the variable does not exist for example) and then check if the variable has the correct type.

like image 68
Aabaz Avatar answered Oct 02 '22 05:10

Aabaz