How to save data from mat file structure into a matrix with for loop

I have data which is produced and stored in a mat file by a function. With the help of a for loop I would like to extract a specific row and store it. At the moment I am only interested in the first row. I would have uploaded the mat file, but it is very large (>1GB), so I will try my best to describe what I am trying to achieve.
The data stored in the mat file can be accessed through oo_.function_results, which is 1000x3 array (see pic below). However, I am only interest in the last column, i.e. three.
You can see the that each of the 1000 rows in column three contain a 12x136x100 array. With a for loop I want to access each of the 1000 rows, and extract every first row of the 12x136x100 array. The problem with the code below is that the entries get overwritten.
So, when , I would like the that the extracted values are just added to the output matrix. For example when switches to 2, then it should be storing the values in row 101, i.e. output_cc(101,:) and so on.
I also tried reshape and vertcat but without any positive result. I would greatly appreciate any help. Thanks.
output_cc = zeros(100000,136);
for ii=1:1000 %loops through the 1000 rows
for yy=1:100 %loops through the third dimension of 12x136x100, i.e. 100
output_cc(yy,:) = xx_.function_results{ii,3}(1,:,yy); %accesses the 12x136x100 array and saves in the output matrix output_cc
end
end

 Akzeptierte Antwort

Try this
output_cc = cellfun(@(x) {squeeze(x(1,:,:)).'}, oo_.function_results(:,3));
output_cc = vertcat(output_cc{:});

4 Kommentare

Hi Ameer, thanks for you help! It seems to have done the job. So, this would replace the for loop approach correct?
Yes, this replaces for-loop. In fact, cellfun works like a for-loop. It is just compact and more intuitive to write it this way.
thanks for your help! Makes sense.
I am glad to be of help!

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (0)

Kategorien

Mehr zu Loops and Conditional Statements finden Sie in Hilfe-Center und File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by