I want to save the data of a variable calculated using script file in a dat file. how do i do it

87 visualizaciones (últimos 30 días)
I want to save the data of a variable calculated using script file in a dat file. so that i can plot it later on
Now how do i do it using matlab. please suggest with the syntax
  2 comentarios
Jan
Jan el 9 de Dic. de 2021
It depends on the type of the variable and how your .dat file should look like. You can store binary or text file in different formats. It matters, if the variable is a struct, a scalar, a 4D array etc.
reshma nesargi
reshma nesargi el 9 de Dic. de 2021
I am performing a simple integral function with in a for loop say t = 20:10:100 and say xx is the output for every value of t. Now i want to save the value of t as well xx for every value of t. Now how to save it

Iniciar sesión para comentar.

Respuestas (4)

Peter Bonavita
Peter Bonavita el 10 de Dic. de 2021
Hi Reshma,
I understand you'd like to save data from MATLAB into a file, then later load that data again to plot it. There are a couple of ways to accomplish this:
The simplest way to save data to a file in MATLAB is with the save command. This will save your data to a .MAT file recognized by MATLAB. From there, you can use the load command to put the variables back in your workspace. Here's a quick example:
t = 20:10:100
xx = 2*t+5
%method 1: use save to write data to .MAT file
save('data.mat',t,xx)
%now load the data back from the .MAT file
load('data.mat')
%plot
plot(t,xx)
Now, if you want your data in a different file format (.TXT, .CSV, or .DAT, for example), one approach is to use writematrix to write the data to the file. Here's an example of that approach, which shows how to use readmatrix to load the data in from the file when you're ready to plot.
%method 2: use writematrix to write to .DAT file
writematrix(t,'t_data.dat')
writematrix(xx,'xx_data.dat')
%now load the data back from the .DAT file
t_file = readmatrix('t_data.dat')
xx_file = readmatrix('xx_data.dat')
%plot
plot(t_file,xx_file)
I hope this helps! Thanks,
Peter

reshma nesargi
reshma nesargi el 10 de Dic. de 2021
Sir i used both the commands suggested by you but it is saving only the last value of the loop
kindly have a look to the file attached here with and suggest.
Anticipating your early reply to help me out
Regards
  1 comentario
Peter Bonavita
Peter Bonavita el 10 de Dic. de 2021
Hi Reshma,
X and Y in your code are scalar variables (with size 1 and only one value) inside the loop.
If you want to save all values of X and Y from each step in the loop, you'll need to put those values into arrays (with size 11). Here's an example below. If you compare the values of loop variable i and array X in your workspace, you'll see the difference in size.
clear
close all
for i = 0:1:10
X(i+1) = i;
Y(i+1) = sin(X(i+1));
end
%writematrix(X,'X_data.dat')
%writematrix(Y,'Y_data.dat')
save('x1.mat', 'X', 'Y')
As an alternative if you run the following code, I'd suggest directly creating X and Y as arrays, which will let you save them to file without using a loop.
X = 0:1:10;
Y = sin(X);
save('x2.mat','X','Y')
>> X
X =
0 1 2 3 4 5 6 7 8 9 10
>> Y
Y =
0 0.8415 0.9093 0.1411 -0.7568 -0.9589 -0.2794 0.6570 0.9894 0.4121 -0.5440

Iniciar sesión para comentar.


reshma nesargi
reshma nesargi el 12 de Dic. de 2021
Thank you so much
but it is storing in rows how to get it in columns.
I need to change the values of magnetic field which does not start necessarly from zero. for example if i want vary field B from 5 tesla to 10 tesla in large number of samples or if i want to vary width say from 50 nm to 150 nm with an increament of 0.1
in that case what about the array position
Is there any method like in C or Fortran where we have open file option where we can store the variable from a loop like what i had sent earlier.
  2 comentarios
Walter Roberson
Walter Roberson el 12 de Dic. de 2021
X = (0:1:10).';
Y = sin(X);
save('x2.mat','X','Y')
X
X = 11×1
0 1 2 3 4 5 6 7 8 9
Y
Y = 11×1
0 0.8415 0.9093 0.1411 -0.7568 -0.9589 -0.2794 0.6570 0.9894 0.4121
Walter Roberson
Walter Roberson el 12 de Dic. de 2021
Is there any method like in C or Fortran where we have open file option where we can store the variable from a loop like what i had sent earlier.
If you want a .mat file, and you want the values all to be stored in the same variable, then the closest you would get to that would be to use the matfile() facility https://www.mathworks.com/help/matlab/ref/matlab.io.matfile.html . I think you will find, though, that it is easier to use save()
If you were looking to create a text file, then there are reasonable ways to add new lines within a loop. Slower than writing everything all at once at the end, but possible.

Iniciar sesión para comentar.


reshma nesargi
reshma nesargi el 13 de Dic. de 2021
Thank you so much
i will go through and revert back to you

Categorías

Más información sobre String Parsing en Help Center y File Exchange.

Etiquetas

Community Treasure Hunt

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

Start Hunting!

Translated by