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

5 views (last 30 days)
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

Accepted Answer

Ameer Hamza
Ameer Hamza on 10 Jun 2020
Try this
output_cc = cellfun(@(x) {squeeze(x(1,:,:)).'}, oo_.function_results(:,3));
output_cc = vertcat(output_cc{:});
  4 Comments

Sign in to comment.

More Answers (0)

Categories

Find more on Loops and Conditional Statements 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!