- Initialize an empty rho array before the loop.
- Inside the loop, read the data from each file and append it to the rho array.
- After every three iterations or when it's the last iteration (k == 12), append the rho array to the rho.mat file without overwriting previously saved data.
- Finally, the rho array is cleared to save memory.
how to modify the saved variable in mat file by adding extra columns without overwriting on the previously saved columns
2 views (last 30 days)
Show older comments
I want to modify mat file created without overwriting the previously saved columns. I want to create an array of 12 columns and each column is corresponding to one file. I want to save the data to the mat file after reading 3 consecutive files, then clear the array (rho) to save memory usage on the computer.
I have written the following lines but the finally saved mat file only contains the last 4 files as it overwrites the saved data on the mat file instead of adding extra columns.
is there any method to modify the code to avoid overwriting old saved data on the mat file?
clc
clear
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
t_inital=0.484913828;
t_final=0.656613828;
delta_t=5e-5;
klimit=(t_final-t_inital)/delta_t;
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%read rho%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
t=t_inital;
m = matfile('rho.mat', 'Writable', true);
for k=2:12
tic
t=t+delta_t;
if k==2
file1=readlines('F:\porous rectangular surface\surface\0.484913828\sampledSurface_sampledSurfaceDict\scalarField\rho');% read ASCII file
rho1=str2double(file1(4:end-2));
%rho=rho1;
elseif k==4||k==7||k==10
save('rho.mat','rho');
clear rho;
file1=readlines(['F:\porous rectangular surface\surface\',sprintf('%.9f',t),'\sampledSurface_sampledSurfaceDict\scalarField\rho']);% read ASCII file
rho1=str2double(file1(4:end-2));
else
rho1=rho;
end
file2=readlines(['F:\porous rectangular surface\surface\',sprintf('%.9f',t),'\sampledSurface_sampledSurfaceDict\scalarField\rho']);% read ASCII file
i=k
rho=[rho1,str2double(file2(4:end-2))];
if mod(k,3) == 0
fprintf('file read %d...\n',k);
tt=toc/60
end
end
0 Comments
Answers (1)
Himanshu
on 21 Feb 2024
To my understanding, you are trying to modify a saved variable in mat file by adding extra columns without overwriting on the previously saved columns.
In the code shared by you, the issue lies in the way the data is saved to the rho.mat file.
elseif k == 4 || k == 7 || k == 10
save('rho.mat','rho');
clear rho;
In this part of the code, you are overwriting the entire rho array every time k equals 4, 7, or 10. This means that only the most recent data stored in rho is saved to the file, and the previously saved data is lost.
To prevent the overwriting of the code, you can follow the following steps:
This way, each iteration appends new data to the existing rho.mat file without overwriting the previous columns.
Hope this helps!
0 Comments
See Also
Categories
Find more on Data Type Conversion 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!