How to save a struct data inside a table ?
8 Ansichten (letzte 30 Tage)
Ältere Kommentare anzeigen
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 Kommentar
Antworten (1)
Arjun
am 11 Okt. 2024
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 Kommentare
Siehe auch
Kategorien
Mehr zu Structures finden Sie in Help Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!