How to save a struct data inside a table ?
7 次查看(过去 30 天)
显示 更早的评论
Hi everyone,
I wrote a program which consists of two for loop (as shown in figure below). The inner for loop will generate a two - structure data (size - 1x7 ) and one - array data( size - 7x1024) for every iteration of the outer for loop.
I tried to save the inner for loop data using struct method. But I am not able to read the data properly.
So, could you please suggest me a way to save both the struct data and array data together to create a table while iterating the outer for loop
or if could suggest me a better and easier approach to save the data.
Thank you.
% Out for loop
for k = 1: 4
% Inner for loop
for i = 1: 7
end
%
end
回答(1 个)
Arjun
2024-10-11
As per my understanding, you want to store struct data and array data being generated by inner for loop into an efficient data structure.
To store and organize struct and table data generated by the inner nested loop, you can use an array of structs where each element of this array is a struct that contains two fields: “structData” and “arrayData”.
Kindly refer to the code below for better understanding of the implementation:
% Create an array of struct for storing the results
results(4) = struct('structData', [], 'arrayData', []);
for k = 1:4
% preallocate space for saving the data
tempStructData = struct('field1', [], 'field2', []);
tempArrayData = zeros(7, 1024);
for i = 1:7
% populate the struct data
tempStructData(i).field1 = rand();
tempStructData(i).field2 = rand();
% Populate the array data
tempArrayData(i, :) = rand(1, 1024);
end
% Store the generated data in the results struct array
results(k).structData = tempStructData;
results(k).arrayData = tempArrayData;
end
% Accessing the data
for k = 1:4
disp(['Data for iteration ', num2str(k)]);
disp(results(k).structData);
disp(results(k).arrayData);
end
This will create the desired structure to efficiently store the data. To convert it into a table will require additional step of converting “structData” and “arrayData” to cell arrays and then creating a table.
Kindly refer to the code snippet below for storing data in a table:
structDataCell = cell(length(results), 1);
arrayDataCell = cell(length(results), 1);
% Populate cell arrays from the struct array
for k = 1:length(results)
% Store each struct and array in a cell
structDataCell{k} = results(k).structData; % Store the struct array
arrayDataCell{k} = results(k).arrayData; % Store the array data
end
resultsTable = table(structDataCell, arrayDataCell, 'VariableNames', {'StructData', 'ArrayData'});
disp(resultsTable);
Kindly read about “cell” arrays and “struct” for more information:
- cell array: https://www.mathworks.com/help/releases/R2021b/matlab/ref/cell.html
- struct: https://www.mathworks.com/help/releases/R2021b/matlab/ref/struct.html
I hope this will help!
0 个评论
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Structures 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!