pre-define a struct/array of structs in attempt to get rid of out of memory error.

1 Ansicht (letzte 30 Tage)
Hello, how can i pre-define an array/struct of structs? Motivation. I was dynamically creating an array that grew with every iteration using the sample code below:
ultimate_result = [];
% then in my loop
for i=1:length(items)
items(i).name = item_name; % string
items(i).size = item_size; % double
items(i).results = item_result; % another struct
ultimate_result = [ultimate_result items(i)];
end
now some of you have already predicted, I run out of memory (which is actually the main problem and reason i need this) as the item size grows even with my 32gb or ram. I know before hand the number of items i will need to process.
I have tried something like
ultimate_result(2000) = struct; %
but MATLAB gives the following error.
Unable to perform assignment because the indices on the left side are not compatible with the size of the right side.
Any suggestions to help get rid of the problem will be greatly appreciated. Of course i have tried increasing matlab priority in the task manager in vain,
Am using windows 10, Matlab 2020a, with about 32gb ram.
  2 Kommentare
Stephen23
Stephen23 am 1 Jun. 2021
"pre-define a struct/array of structs in attempt to get rid of out of memory error"
Preallocating a structure will not reduce its memory consumption.
MatlabEnthusiast
MatlabEnthusiast am 4 Jun. 2021
@Stephen Cobeldick indeed the out of memory error persists. What are my options I suspsect that i do indeed run out of memory.

Melden Sie sich an, um zu kommentieren.

Akzeptierte Antwort

Matt J
Matt J am 1 Jun. 2021
Bearbeitet: Matt J am 1 Jun. 2021
It is not at all clear from what you posted why you might run out of memory, nor why you get the error message you have posted, which has nothing to do with memory limits.
However, assuming "ultimate_ressult" is a typo and should really be "ultimate_result", then you are needlessly updating ultimate_result inside your loop and slowing things down. The whole thing, with proper pre-allocation, can be done as follows.
N=___ % number of "items"
items(N).name=[];
items(N).item_size=[];
items(N).item_result=[];
% then in my loop
for i=1:N
items(i).name = item_name; % string
items(i).size = item_size; % double
items(i).results = item_result; % another struct
end
ultimate_result=items;
  3 Kommentare
MatlabEnthusiast
MatlabEnthusiast am 4 Jun. 2021
The pre allocations worked ffine, thanks, but the out of memory error persists. What are my other optiions store this information in files or what?
Matt J
Matt J am 4 Jun. 2021
The memory error isn't coming from code that I suggested to you. There are no horzcat operations in the version I posted.

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (1)

Joseph Cheng
Joseph Cheng am 1 Jun. 2021
the pre-allocation can be done (though can't really find the right way
ultimateR = [];
temp = struct('name',[],'size',[],'results',[]);
temp(2000).name = 'dummy'; %to be overridden when you actually get to index 2000
for ind = 1:10
temp(ind).name = 'dummy';
temp(ind).size = 10;
temp(ind).results = temp(1);
ultimateR = [ultimateR temp(ind)];
end
I don't think you need your line ultimate_ressult as you're already saving the data in items(i).'parameter'. you're doubling the stored data by re-saving it into ultiate_results which the data is contained in items. i don't get the same error but perhapse something is predefined such that the left and right hand some of one of the above lines isn't right.

Produkte


Version

R2020a

Community Treasure Hunt

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

Start Hunting!

Translated by