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!
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With