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

5 views (last 30 days)
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

Answers (1)

Image Analyst
Image Analyst on 25 Jun 2022
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)

Tags

Products


Release

R2021b

Community Treasure Hunt

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

Start Hunting!