MATLAB: How to use “run command” within a function which saves all variables inside base workspace
function example()
%%%%
---
run('sample.m');
---
end
Is there any way, which can help to save all variable of "sample.m" script in base workspace rather than current function workspace?
Best Answer
No. run isdefined 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 besome 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
Use Simset. See details here. The 'DstWorkspace' property specifies the workspace in which to assign any variables defined in the model. The default is the current workspace. Change the destination workspace to 'Base'. Hope this helps!!
Best Answer