I have made a menu, instead of me asking the same questions at the start of each matlab file. would it be possible using run command to pass over a variable. If this is possible would it be possible to pass variables back from the running m file to the menu ?
MATLAB: Pass variable’s through the run command
MATLABmenurun
Related Solutions
Before you evalin('base', 'untitled'), use evalin('base') to run a "whos" in the base workspace. Assign the result to a variable.
Then run your evalin('base', 'untitled')
Then again use evalin('base', 'whos') and assign the result to a different variable.
Now take the two whos() results and compare them. Any variable that exists in the second result but not in the first is definitely an output of the script and can be displayed by using evalin('base') to display the value. For any variable that existed in both results, check to see if the sizes or types are different: if they are, then again the variable is an output variable.
Now you are at the point of checking two variables in the same name and same size and same type, and you want to know whether the variable is an output variable or not. Unfortunately, if the sizes and types are the same, whos() cannot tell you that.
So what you should do before running your 'untitled' script is something like use a serialization routine on each variable that exists in the base workspace at the time you start, and then do something like MD5 on the serialized bytes, and store the MD5 signature. Then you would so the same thing afterwards, and compare the MD5 signatures: if they are different, then the variable was an output variable.
Unfortunately, having the same MD5 signature is not proof that the two values are the same. You could compare the serialized versions of them for differences.
Might I suggest to you that it would be considerably easier if you were to instead use a function, rather than having to figure out after the fact which variables the script modified?
No. run is defined as executing a script, and scripts always have access to the current workspace and affect the current workspace (though in some very recent MATLAB versions there are starting to be some ways in which scripts do not have full access to private information.)
You could create a wrapper function that looked in its parent function and copied all of the parent's variables into the current workspace, and then ran the script, and then looked at all remaining variables and compared them to the parent's copies of the variables in order to determine which had changed, and then write the changed versions to the base workspace. This would be somewhat troublesome, but I guess there might be some use for it.
If you did not want the script to have access to any of the current workspace's variables while it ran, then you could
evalin('base', 'run(''script.m'')')
Best Answer