Propagate Data Within Structure Array Based on Pre-Existing Table
3 views (last 30 days)
Show older comments
Hello,
I set up a Structure Array to have its size dependent on the imported data.
To keep it simple, I'll say it has 4 cells (1x4 sized Structure Array).
Each cell is propagated with a zeroed table that matches the rows x column size of the imported data.
Again, to keep it simple, I'll say that each cell has a 1000 x 10 table (so four tables in the structure array).
What I want to do is that have the zeroed tables in each cell of the structure array hold different data from the imported data.
What I have right now is this:
for i=1:4 % Number of Cells
for j=1:1000 % Number of Rows
for k=1:10 % Number of Columns
if i==1
Structure_Array.Title{i}(j,k) = Imported_Table(j,k)
else
% I'll have simialar statements like above here (using elseif)
% I'll also use a while loop or other criteria to only take
% certain data from the Imported Table.
% For right now, this is sufficient enough for my question.
end
end
end
end
In my head, I expect this above script to leave the 2nd, 3rd, and 4th cell of the structure array alone (keeping them all zeros of size 1000 x 10).
And it will basically replace the 1st cell's table with a copy of the Imported Table.
Instead, it replaces every cell by a vector of zeroes of length 1000 (1000x1).
(Ultimately I plan to only take certain rows of the Imported Table in each cell of the structure array.
For example, cell 1's table will have rows 1-550, cell 2 will have 551-890, cells 3 will have 891-930, and cell 4 will have 931-1000.)
Any help is appreciated, and I can explain further if my message isn't clear.
Thanks.
4 Comments
Stephen23
on 7 Dec 2022
Edited: Stephen23
on 7 Dec 2022
"Why would it not still work all the same though?"
The main benefits of a structure array (compared to a scalar structure with nested cell array) that come to mind:
- only one array, not multiple arrays (slight memory/efficiency benefits possible),
- very useful ability to use comma-separated lists: https://www.mathworks.com/matlabcentral/answers/1656435-tutorial-comma-separated-lists-and-how-to-use-them
"Does my logic make sense?"
It generally seems okay.
However it is unclear why you need nested loops to achieve this, when MATLAB is designed for handling matrices and arrays. This does not seem to be the best use of MATLAB.
Does your structure only have one field? If so, why not just use a cell array?
Is Imported_Table really table class? If so, then that indexing using parentheses would also return scalar tables, which are unlikely to be very useful. I suspect that Imported_Table is really some kind of numeric matrix, but this is just a guess.
Answers (0)
See Also
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!