Clear Filters
Clear Filters

How can I import workspace arguments into a function without saving them?

6 views (last 30 days)
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 Comment
Stephen23
Stephen23 on 18 Jul 2023
"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.

Sign in to comment.

Accepted Answer

Steven Lord
Steven Lord on 17 Jul 2023
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
See this documentation page for more information.
  3 Comments
Steven Lord
Steven Lord on 17 Jul 2023
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.
Rabih Sokhen
Rabih Sokhen on 18 Jul 2023
Alright, I agree to try the following approach.
Thank you.
Best regards,
Rabih EL SOKHEN

Sign in to comment.

More Answers (0)

Community Treasure Hunt

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

Start Hunting!