I want to save the data of a variable calculated using script file in a dat file. how do i do it
56 views (last 30 days)
Show older comments
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 Comments
Jan
on 9 Dec 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.
Answers (4)
Peter Bonavita
on 10 Dec 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
0 Comments
reshma nesargi
on 10 Dec 2021
1 Comment
Peter Bonavita
on 10 Dec 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
reshma nesargi
on 12 Dec 2021
2 Comments
Walter Roberson
on 12 Dec 2021
X = (0:1:10).';
Y = sin(X);
save('x2.mat','X','Y')
X
Y
Walter Roberson
on 12 Dec 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.
See Also
Categories
Find more on String Parsing 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!