Loading variables to a specifc workspace
9 次查看(过去 30 天)
显示 更早的评论
I am trying to use the load command to load some variables into my workspace.
because the load command is used inside a function call, it is loading the data to the workspace of the function and not loading it to my base for example.
How can i use the load command to load variables to a specific workspace
0 个评论
回答(3 个)
Star Strider
2023-12-8
One option to transfer specific variables to your script (calling) workspace would be to include them as outputs of your function.
You could of course load them into your workspace with a separate load call.
6 个评论
Steven Lord
2023-12-9
I'm not 100% certain off the top of my head what the upper limit on the number of output arguments you can return from a function, but even if that limit is greater than 500 I agree with your premise that you should not return 500 outputs from a function.
Instead, pack the pieces of data that you want to return from the function into a container: a struct array, a table, a cell array, an object, or even a dictionary array. Then return that container, with the data organized inside it, from your function.
Walter Roberson
2023-12-9
Assign the result of load() to a variable; the result will be a struct with one field for each variable name. Pass out the struct.
If you are loading multiple files in the same routine, then I start to be concerned about the possibility that the same variable might exist in more than one file, and what the desired behaviour is in that situation.
Walter Roberson
2023-12-8
How can i use the load command to load variables to a specific workspace
MATLAB does not provide any way to do that.
I would provide work-arounds, but you made it clear to Star Strider that you only want answers to questions that you actually asked, rather than solutions that would solve the challenge you are facing.
0 个评论
Steven Lord
2023-12-8
How can i use the load command to load variables to a specific workspace
In general, you cannot do exactly what you asked. There's no functionality to say "load variables into the workspace of the function call at level 3 of the current calling stack", for instance.
You could do what you described actually wanting to do in your comment on Star Strider's answer, but I strongly discourage it. The assignin function allows you to assign values to variables in the workspace of the current stack frame's caller or in the base workspace.
However, it sounds like you're planning to load an arbitrary number of arbitrarily named variables into that workspace and "poofing" variables like that is a Bad Idea. If you do that there is no safeguard against overwriting existing variables with the same name as variables in your base workspace. If your program then "reaches" into that workspace to access that data and the data isn't what you expected, trying to debug whatever happens could be difficult and/or time consuming to diagnose where the bug in your code originated. This is especially true if you were to load multiple files that way -- which of those files had the variable with the errant value?
Creating hundreds of variables in the base workspace also makes it more difficult to find the data you actually need or want to access.
0 个评论
另请参阅
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!