Extracting Data from an Array of Arrays using a Loop?
显示 更早的评论
Hello All,
Context: I am using Simulink and Simscape to simulate some system responses to motion. Simulation output writes to the Matlab workspace to an Array of Arrays (object maybe? Not super sure if that's the right nomenclature) called "out". Our model is set up such that it iterates over a range of values for a single parameter. One test might have 5 iterations, the next might have 50, and so on. This means "out" changes size each time we run our model.
Problem: Now on to the issue. I am trying to extract data from each iteration and put them all into a Matrix. I can access all the data I want manually, using the notation "out(1,1).RollAngle.Data" for the first iteration, "out(1,2).RollAngle.Data" for the second iteration, and so on. However, this is too cumbersome since most of our tests are going to have 20-plus iterations.
Can anybody help me create a for loop that would append each data series as a new column in a matrix? This might be super obvious, but it's a new problem for me...

Thanks!
采纳的回答
更多回答(1 个)
Umar
2024-6-14
0 个投票
we can create a for loop in Matlab that iterates through each element in the "out" array, extracts the desired data, and appends it as a new column in a matrix. This process will automate the extraction and organization of data, making it more manageable and scalable for varying numbers of iterations.
Here is a step-by-step guide to achieve this:
Initialize Variables: Before the loop, initialize an empty matrix to store the extracted data columns. Also, determine the total number of iterations to set the size of the final matrix.
numIterations = size(out, 2); % Get the total number of iterations dataMatrix = []; % Initialize an empty matrix to store data columns
For Loop: Create a for loop that iterates through each iteration in the "out" array. Within the loop, extract the desired data and append it as a new column to the data matrix.
for i = 1:numIterations dataColumn = out(1, i).RollAngle.Data; % Extract data for the current iteration dataMatrix = [dataMatrix, dataColumn]; % Append data as a new column in the matrix end
Complete Code: Combine the initialization, for loop, and any additional processing steps into a complete script or function.
numIterations = size(out, 2); % Get the total number of iterations dataMatrix = []; % Initialize an empty matrix to store data columns
for i = 1:numIterations dataColumn = out(1, i).RollAngle.Data; % Extract data for the current iteration dataMatrix = [dataMatrix, dataColumn]; % Append data as a new column in the matrix end
% Additional processing or analysis can be performed on the dataMatrix disp(dataMatrix); % Display the final matrix
By following these steps and customizing the data extraction based on the structure of the "out" array and the specific data fields needed, you can efficiently extract and organize simulation data into a matrix format. This approach streamlines the process and eliminates the manual effort required for each iteration, enhancing the scalability and usability of your simulation data analysis.
To the best of my knowledge, this will help you figure out solution to your problem.
5 个评论
Kylen
2024-6-14
Umar
2024-6-15
In Matlab, when concatenating arrays using square brackets [ ], the arrays are automatically padded with zeros to match the length of the longest array. Therefore, when appending dataColumn to dataMatrix, Matlab will handle the varying lengths by padding the shorter columns with zeros to align with the longest column. This behavior ensures that the final matrix dataMatrix will have consistent dimensions, allowing for further processing or analysis without issues related to different data lengths.
"In Matlab, when concatenating arrays using square brackets [ ], the arrays are automatically padded with zeros to match the length of the longest array. "
That's not true.
a = [1;2]
b = 1;
c = [a b]
Umar
2024-6-15
The error message Error using horzcat Dimensions of arrays being concatenated are not consistent indicates that there is a mismatch in the dimensions of the arrays being concatenated. In this case, a is a 2x1 array, and b is a scalar, which leads to an inconsistency when trying to concatenate them horizontally
Paul
2024-6-15
Exactly. Point being that there is no zero padding if there is a size mismatch between the arrays being concatenated.
类别
在 帮助中心 和 File Exchange 中查找有关 Data Type Conversion 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!