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

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 commentaires

Although you have already accepted an answer, I very much doubt that you need to (inefficiently) save and load data each time you call some function. Most likely you could just use output arguments or perhaps a nested function, as it seems that your goal is actually to pass data from the function workspace to the calling workspace.
You would be better off actually describing what your goal is, then you are likely to get solution that actually help you to write better code.

Connectez-vous pour commenter.

 Réponse acceptée

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 commentaires

you should initialize counter variable in your main file.
clc
clear
global counter
counter=1;
..
..
..
call function
..
..
..
thanks for your answer, but i did not get it, here it is a basic function. i would like to save all "w" values (it is changing after each new "x" value). can you explain that on that?
%%%%%%
function f= F_xy(x,y)
if x>4
w=30;
else
w=20;
end
f=3.*exp(-x)-0.4*y+w; % change the function as you desire
end
%%%%%%%
You're welcome.
Yeah you can use x instead of counter. use this:
%%%%%%
function f= F_xy(x,y)
if x>4
w=30;
else
w=20;
end
f=3.*exp(-x)-0.4*y+w; % change the function as you desire
str=['w_',num2str(x),'_',num2str(y),'.mat'];
save(str,'w')
end
%%%%%%%
For example if you use F_xy(5,8) , you have a file w_5_8.mat
i have tried that but it still keep giving me only one value which is w=20.. (please keep in mind, i am using Runge Kutta as a solver ). here it is the rest of the code. i have used this basic code before applying into my original code. please see that. thanks
clc;
clear all;
h=0.5; % step size
x = 0:h:3; % Calculates upto y(3)
y = [zeros(1,length(x))];
for i=1:(length(x)-1) % calculation loop
k_1 = F_xy(x(i),y(i));
k_2 = F_xy(x(i)+0.5*h,y(i)+0.5*h*k_1);
k_3 = F_xy((x(i)+0.5*h),(y(i)+0.5*h*k_2));
k_4 = F_xy((x(i)+h),(y(i)+k_3*h));
y(i+1) = y(i) + (1/6)*(k_1+2*k_2+2*k_3+k_4)*h; % main equation
end
After the run, you should have several m-files in your current folder.
If you want to see the values you should use load command for each one.
"...please keep in mind, i am using Runge Kutta as a solver"
What does any of this have to do with saving and loading files? Why not just pass data directly as output arguments, or use nested functions?
i see, thanks, i got it, i do have 60 000 data so that this method will not be practical, how can i combine them and make just a "mat" file, is that possible ? cheers.
Stephen23
Stephen23 le 21 Nov 2018
Modifié(e) : Stephen23 le 21 Nov 2018
"...how can i combine them and make just a "mat" file, is that possible ?"
All sorts of things are possible, but because your question only talks about saving variables, we have no idea what you are actually trying to do.
If you want help then you need to be much more descriptive aqbout what you are doing: what data do you have, what functions are you calling, how do you pass any data, ... and then explain what your goal is that you want to achieve.
If I am not wrong, each "w" is a variable. I want to plot those "w" s (in fact, i will get about 60 000 data. this is just a basic code. I do have pretty complicated code). just wanted to see how this "w" changes by the "x" value
let's say , i will plot
while x=0 w=..
x=1 w=..
x=2 w=..
so on
.
.
I just want to plot this relation. Therefore, I need to combine those all value in a 1 "mat" file. Just for being able to plot that easily. Hope it is clear. cheers.
Stephen23
Stephen23 le 21 Nov 2018
Modifié(e) : Stephen23 le 21 Nov 2018
@ADNAN KIRAL: so just store the values in an array, then plot them.
It seems all you need is to store the data in an array using basic indexing. What is stopping you from doing that?
yes i need to store that but how ? basic indexing ? really did not get. thanks.
can anyone please answer my question ?
First, modify your function:
function [f,w]= F_xy(x,y)
if x>4
w=30;
else
w=20;
end
f=3.*exp(-x)-0.4*y+w; % change the function as you desire
end
Now you have 2 outputs.
Then tou can change main file like this:
clc;
clear all;
h=0.5; % step size
x = 0:h:3; % Calculates upto y(3)
y = [zeros(1,length(x))];
for i=1:(length(x)-1) % calculation loop
[k_1(i), w_1(i)] = F_xy(x(i),y(i));
[k_2(i), w_2(i)] = F_xy(x(i)+0.5*h,y(i)+0.5*h*k_1);
[k_3(i), w_3(i)] = F_xy((x(i)+0.5*h),(y(i)+0.5*h*k_2));
[k_4(i), w_4(i)] = F_xy((x(i)+h),(y(i)+k_3*h));
y(i+1) = y(i) + (1/6)*(k_1(i)+2*k_2(i)+2*k_3(i)+k_4(i))*h; % main equation
end
You can also plot k or w like this:
figure, plot(k_1)
figure, plot(k_2)
figure, plot(k_3)
figure, plot(k_4)
figure, plot(w_1)
figure, plot(w_2)
figure, plot(w_3)
figure, plot(w_4)

Connectez-vous pour commenter.

Plus de réponses (1)

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.

Catégories

En savoir plus sur Data Type Identification dans Centre d'aide et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by