How to save part of the data in text file?

1 view (last 30 days)
I have the following plots at different time steps, I would like to save the data at the last time step in a text file. How can I simply do that?
For example my plotting section, I am saving the data into a multi-dimenional array that has the 3rd dim as t and would plot my data in three different timesteps. How can I save data in a text file at timestep 0.12 at the plots example and not the entire data.
Code:
t = 0;
step = 0;
hstep = 1;
wstep = 1;
while t < tend && step < N_max
[U_T, t] = MacCormack(U_T, gamma, t, dx, CFL, sigmma);
step = step + 1;
if mod(step,wstep) == 0
U_TData(:,:,hstep) = U_T; % How to save this data in text file?
Vp_Data(:,:,hstep) = C2P( U_T, gamma );
hstep = hstep+1;
t_Data(hstep) = t;
end
end
shiste = size(U_TData);
numframe = shiste(3);
iterframe = floor(numframe/3);
f1 = figure();
subplot(2,2,1)
for i = 1:3
plot(x,U_TData(1,:,i*iterframe))
hold on
Legend{i} = strcat('t=', num2str(t_Data(i*iterframe),'%.2f'));
end
Plots:

Accepted Answer

Scott MacKenzie
Scott MacKenzie on 8 May 2021
Edited: Scott MacKenzie on 8 May 2021
If you want to save the data being plotted on the last iteration (i = 3), try adding one line after your loop:
for i = 1:3
plot(x,U_TData(1,:,i*iterframe))
hold on
Legend{i} = strcat('t=', num2str(t_Data(i*iterframe),'%.2f'));
end
writematrix([x U_TData(1,:,3*iterframe)], 'name_of_file.txt');

More Answers (0)

Community Treasure Hunt

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

Start Hunting!

Translated by