Saving cell to file to save RAM

2 次查看(过去 30 天)
Jordan Coombs
Jordan Coombs 2020-12-3
评论: Stephen23 2024-9-6
I am trying to run this code to give me my values in a cell, however i am dealing with a large amount of data and it usually crashes around the 6th iteration of j. I was wondering is there any way i can optimise this or save to file line by line to save RAM. I am very new to matlab so any help is appreciated.
j = 1;
while j <= 4
%inputdir = 'C:\Users\jorda\Desktop\Physics Project\Country PM25';
%outputfile = 'Africa_With_Dust'; %do not put it into inputdir !
%dinfo = dir( fullfile(inputdir, '*.mat') );
%filenames = fullfile( inputdir, {dinfo.name} );
PM_val = ncread(With_Dust_List(j),'PM25');
i = 1;
j = j + 1;
while i <= 51
mat_zer=zeros(2600,7200);
isin1 = inpolygon(LON,LAT,S(i).X,S(i).Y);
mat_zer(isin1)=st(i);
[row1,col1] = find(mat_zer~=0);
mat_zer_pm = PM_val;
inds1 = sub2ind(size(mat_zer_pm),row1,col1);
new_mat1 = nan(size(mat_zer_pm));
new_mat1(inds1) = mat_zer_pm(inds1);
Country_With{i,j} = new_mat1;
i = i + 1;
end
%for K = 1 : length(filenames)
%thisfile = load(filenames{K});
%save(outfile, '-struct', 'Country_With{1:51,j-1}', '-append');
%end
end

回答(1 个)

Deepak
Deepak 2024-9-6
To my understanding, you have written a MATLAB code to process a large amount of input data and store the output data in a .mat” file. However, the code crashes on the 6th iteration of the for loop as the output data is being appended to a cell array in each iteration.
To optimize the code, we can append the output data to a mat file in each iteration, that will keep the size of the output cell array limited. Additionally, we can clear the unnecessary variables after each iteration to save the memory.
Below is the updated MATLAB code that addresses the task:
inputdir = 'C:\Users\jorda\Desktop\Physics Project\Country PM25';
outputfile = 'Africa_With_Dust.mat'; % Ensure this is not in inputdir!
j = 1;
while j <= 4
PM_val = ncread(With_Dust_List(j), 'PM25');
j = j + 1;
for i = 1:51
mat_zer = zeros(2600, 7200);
isin1 = inpolygon(LON, LAT, S(i).X, S(i).Y);
mat_zer(isin1) = st(i);
[row1, col1] = find(mat_zer ~= 0);
% Initialize the PM value matrix and new matrix
mat_zer_pm = PM_val;
inds1 = sub2ind(size(mat_zer_pm), row1, col1);
new_mat1 = nan(size(mat_zer_pm));
new_mat1(inds1) = mat_zer_pm(inds1);
% Save the data for the current region and dataset
varName = ['Country_With_', num2str(i), '_', num2str(j-1)];
save(outputfile, varName, 'new_mat1', '-append');
% Clear variables to free memory
clear mat_zer mat_zer_pm new_mat1 row1 col1 inds1;
end
end
Please find attached the documentations of “save” and “clear” function in MATLAB for reference:
I hope this resolves the issue.

类别

Help CenterFile Exchange 中查找有关 Startup and Shutdown 的更多信息

产品


版本

R2020b

Community Treasure Hunt

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

Start Hunting!

Translated by