Effacer les filtres
Effacer les filtres

m file is not reading variables from the workspace

24 vues (au cours des 30 derniers jours)
Francisco
Francisco le 14 Juil 2011
Modifié(e) : BA Lamine le 22 Avr 2015
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
  2 commentaires
Chirag Gupta
Chirag Gupta le 14 Juil 2011
Ensure that its a script and not a function.
Francisco
Francisco le 14 Juil 2011
Hi Gupta,
It is actually a function, is there a way to bypass this problem? there must be a way to call the variables into the function file correct?

Connectez-vous pour commenter.

Réponse acceptée

Chirag Gupta
Chirag Gupta le 14 Juil 2011
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
  1 commentaire
Francisco
Francisco le 14 Juil 2011
Hi Gupta,
I already tried saving the data into a mat file and then loading it to the function file, but it didn't work! :( I'll try varname and hope that works. I was also told about assignin, so I'll try that too! :)

Connectez-vous pour commenter.

Plus de réponses (3)

Walter Roberson
Walter Roberson le 14 Juil 2011
"current" is probably being defined in the workspace of a different function, unless that other function is using assignin('base',...)
  1 commentaire
Francisco
Francisco le 14 Juil 2011
Hi Walter,
That is correct, the variable current is acquired from a different script and it is located in the base workspace. My file is a function file and it only works if I physically copy and paste the variables into the file. I've never used assignin but I'll look into it and I hope this can solve the problem. Thanks! :)

Connectez-vous pour commenter.


Francisco
Francisco le 14 Juil 2011
Hey guys, I tried loading the data again after noticing a mistake I had and voila, the file works!! :) Thanks for your help and for future reference, yes, saving the variables in a MAT file and then loading them to the function file does work!! :)
Thanks guys!!
Francisco

BA Lamine
BA Lamine le 22 Avr 2015
Modifié(e) : BA Lamine le 22 Avr 2015
Hello Gupta,
I tried to use the instruction
variableName_in_myFunction = evalin('ws', variableName_in_workspace);
the instruction doesn't work in the simulation in Simulink. Here the error that I had when I try to run the simulation : Error evaluating registered method 'Outputs' of M-S-Function myFunction in mySimulinkModel. Undefined function or variable variableName_in_myFunction.
Does someone had this error?
Would you please want to help me?
Thank you in advance and your collaboration.
Lamine

Catégories

En savoir plus sur Standard File Formats dans Help Center et File Exchange

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by