What is the for loop that I can use to do the following then save the results as .mat file?

What is the for loop that I can use to do the following then save the results as .mat file?
I have 30 iterations and in each iteration I want to save the following:
Image with size 120*120 *1* 6000
Labels with size: 1*6000
E= 1*6000
and I will repeat the 30 iterations for 12 experiments (12 run).
Which means in each run will get the above files 30 times

4 Comments

Do you want to save them in an excel file? If so, do you want separate files for each data-set - image, labels and E?
If not, then please specify.
How do you obtain this data and what is the data type of each object?
Maybe a 12 x 30 x 120 x 120 x 1 x 6000 matrix for the images, a 12 x 30 x 6000 matrix for the labels and a 12 x 30 x 1 x 6000 matrix for E ?
@Dyuman Joshi, I want save them as .mat files for further analysis.

Sign in to comment.

 Accepted Answer

Preallocate arrays according to the final size and the data type and modify the arrays iteratively.
Use indexing to obtain the corresponding data.

8 Comments

@Dyuman Joshi I tried to use the indexing but if failed, there is a mistake but I can't figure, Then I tried to save each iteration for each run separately, But it was too slow process and not efficient at all.
Can you please suggest a code for the above variable with the a random dimensions
Thanks
@M, How are you running the 12*30 iterations? A double for loop?
The 12 run will be run individually,
each run contains 30 iterations
Suggest for me (for loop) only for one run which contains 30 iteration, the result of each iteration:
Image with size 120*120 *1* 6000 4D double
Labels with size: 1*6000 double
E= 1*6000 double
The final image, Labels and E, should contain the all results of 30 iterations for one run
For loop for only 1 single run -
%number of iterations
iter = 30;
%preallocation
data_image = zeros(iter,120,120,1,600);
Labels = zeros(iter,1,600);
E = zeros(iter,1,600);
for k = 1:iter
data_image(k,:,:,:,:) = newImage;
Labels(k,:,:) = newLabels;
E(k,:,:) = newE;
end
@Dyuman Joshi Thanks, But I have question please
If we don't know the number of image and we put initially 1000 like the following
data_image = zeros(iter,120,120,1,1000);
Then after run it was 5000, will the other 4000 be saved and the data_image will be updated or not?
Yes, the other 4000 will be saved, however, the process will be a bit slower as MATLAB will have to find continous memory to store new data from iteration 1001 onwards.
If you don't know the final size of the output, you can pre-allocate a cell array instead -
iter = 30;
C = cell(iter,1);
for k=1:iter
C{k} = store_data;
end
And concatenate the data after the for loop (assuming data is compatible for concatenation)
@Dyuman Joshi Thanks the 2nd solution worked but when I tried to save the results I got the following:
Warning: Variable 'Images' was not saved. For variables larger than 2GB use
MAT-file version 7.3 or later.
What can I do in this case?
@M - Does your PC have a 64 bit processor?
Also, check the setting of version of MAT-Files in your MATLAB - https://in.mathworks.com/help/matlab/import_export/mat-file-versions.html
Make sure the selected version is MAT-File Version 7.3

Sign in to comment.

More Answers (0)

Categories

Find more on Convert Image Type in Help Center and File Exchange

Asked:

M
M
on 3 Nov 2023

Edited:

on 6 Nov 2023

Community Treasure Hunt

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

Start Hunting!