how can save all variables which are in function without deleting previous one ?

29 views (last 30 days)
how can save all variables which are in function ? i would like to extract those variables out of function. if i just use "save" command in function, it will delete just previous variable. however, i need all of them not the last one. can anyone help
  2 Comments

Sign in to comment.

Accepted Answer

Majid Farzaneh
Majid Farzaneh on 21 Nov 2018
Hi.
You can make a string in each run for your function and then use the string for your file name. for example you may use a global counter in function like this:
function ....
global counter
counter=counter+1; %
str=['file' , num2str(counter) , '.mat'];
...
...
...
save(str)
end
  15 Comments

Sign in to comment.

More Answers (1)

Cris LaPierre
Cris LaPierre on 21 Nov 2018
The specifics are a little unclear, but here are some options.
Consider saving them to a mat file with a unique filename. However, they will still contain the same variable names, so when loaded back into the workspace, will replace any existing variables with the same names.
You could return them through the function output. That allows you to assign them to variables with whatever name you want, allowing you to have both at the same time.
If there are a lot of variables, consider using a structure so you only have to pass out one variable.

Categories

Find more on Variables in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!