MATLAB: M file is not reading variables from the workspace

evalinlsqnonlin

Hello,
I'm having problems with my script, it doesn't want to read the variables from the workspace. I tried several things, among them evalin and it still doesn't work. I don't want to have to copy the data and physically paste it to the script, does anybody knows how to solve this problem? I keep getting the error current has not been assigned, although the variable current is in the workspace already (this variable was created by another file). How can I call current from the workspace so the m file can read the variable?
Here is a short version of my file and I'm omitting the equations as they are pretty long.
Jphotototal=current(1,:); plot(lambda,Jphotototal,'ro'); hold on L0=[0.1 0.1]; options = optimset ('Maxiter',1000,'tolfun',1e-20,'TolX',1e-20,'MaxFunEvals',1000); L=lsqnonlin('recfun',L0,0,400,options);
Thanks, Francisco

Best Answer

Functions have their own workspaces. Scripts on the other hand run in the base workspace where you have your data.
You could always save your workspace data into a MAT file:
save ('filename','variable Name');
and then in your function use the command:
load('filename')
Apart from this you can use the following command:
VarName = evalin('base','VarNameFromBase')
and then use VarName in your function