フィルターのクリア

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

6 ビュー (過去 30 日間)
Rabih Sokhen
Rabih Sokhen 2023 年 7 月 17 日
編集済み: Walter Roberson 2023 年 7 月 18 日
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
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
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
See this documentation page for more information.
  3 件のコメント
Steven Lord
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.
Rabih Sokhen
Rabih Sokhen 2023 年 7 月 18 日
Alright, I agree to try the following approach.
Thank you.
Best regards,
Rabih EL SOKHEN

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeWorkspace Variables and MAT-Files についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by