How to save two variables and create a function to use it later

1 回表示 (過去 30 日間)
David
David 2022 年 6 月 25 日
回答済み: Image Analyst 2022 年 6 月 25 日
Hello, I have the following question, how can I create a function from two variables and then call it, for example I have the time that is a variable 'x' of 1000x1 and another variable of 'y' of the same size, these are data columns . I need to save this data to later call it in a function

回答 (1 件)

Image Analyst
Image Analyst 2022 年 6 月 25 日
Try this to save your two vectors in a .mat file and then recall them later
% Save time and y variable
save('Time Data.mat', 'x', 'y');
% Now recall it. You can have this in a different script or the same script.
s = load('Time Data.mat'); % s is a structure variable.
% Extract the fields into individual vectors.
x = s.x;
y = s.y;
To create a function that will use those two variables:
function someResults = UseXY(x, y)
% Do something with x and y, such as
plot(x, y, 'b-', 'LineWidth', 2);
% Optionally return something.
someResults = true; % Whatever you want.
Now to call the function from a script or other function:
% Define x and y somehow first, then call UseXY:
someResults = UseXY(x, y)

カテゴリ

Help Center および File ExchangeLanguage Support についてさらに検索

タグ

製品


リリース

R2021b

Community Treasure Hunt

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

Start Hunting!

Translated by