How can I import workspace arguments into a function without saving them?
6 次查看(过去 30 天)
显示 更早的评论
Hey guys,
I would like to create a function that directly uses arguments from the workspace without the need to save and reload the workspace. Do you have any ideas on how to achieve this?
Currently, I have written a small example where I only call one argument. However, the actual code contains many arguments, and the process of saving and reloading them is time-consuming for the treatment.
Thank you in advance.
Best regards,
Rabih EL SOKHEN
Code:
clear all
clc
a = randi(5, 5, 5);
show_matrix
function show_matrix
evalin('base', 'save myvars.mat');
load myvars.mat;
pcolor(a)
end
I hope this helps! Let me know if you have any further questions.
1 个评论
Stephen23
2023-7-18
"I have over 100 arguments..."
The store them in one structure. Then pass that one structure as an input argument. Easy.
Don't make your code (and accessing your data) more complex than it needs to be.
采纳的回答
Steven Lord
2023-7-17
I would like to create a function that directly uses arguments from the workspace without the need to save and reload the workspace. Do you have any ideas on how to achieve this?
Yes. Pass those arguments into your function as input arguments.
a = randi(5, 5, 5);
show_matrix(a)
function show_matrix(a)
pcolor(a)
end
3 个评论
Steven Lord
2023-7-17
IMO a function with 100 arguments is going to be effectively unusable. The example I use most often is the fmincon function which has, in its longest documented syntax, 10 input arguments and users omit some of the intermediate inputs all the time.
If the data you want to pass into the function can be combined into several groups of related pieces of data, I would put them in one or more containers (struct, table, cell, dictionary, object, etc.) from the start (don't create them as independent variables in the first place!) and pass the containers into the function.
If it can't, this smells an awful lot like your function is trying to do way too much, in violation of the single responsibility principle.
更多回答(0 个)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Whos 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!