Filter löschen
Filter löschen

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

6 Ansichten (letzte 30 Tage)
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 Kommentar
Stephen23
Stephen23 am 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.

Melden Sie sich an, um zu kommentieren.

Akzeptierte Antwort

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

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (0)

Kategorien

Mehr zu Workspace Variables and MAT-Files finden Sie in Help Center und File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by