Effacer les filtres
Effacer les filtres

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

6 vues (au cours des 30 derniers jours)
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 commentaire
Stephen23
Stephen23 le 18 Juil 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.

Connectez-vous pour commenter.

Réponse acceptée

Steven Lord
Steven Lord le 17 Juil 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 commentaires
Steven Lord
Steven Lord le 17 Juil 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 le 18 Juil 2023
Alright, I agree to try the following approach.
Thank you.
Best regards,
Rabih EL SOKHEN

Connectez-vous pour commenter.

Plus de réponses (0)

Catégories

En savoir plus sur Workspace Variables and MAT-Files dans Help Center et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by