Propagate Data Within Structure Array Based on Pre-Existing Table

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

"To keep it simple, I'll say it has 4 cells (1x4 sized Structure Array)."
A structure array does not have cells, it has elements (just like every array type). Cell arrays have cells.
Your code here:
Structure_Array.Title{i}(j,k) = ...
indicates that you actually have a scalar structure (i.e. 1x1, not a structure array as you wrote) containing one cell array. You are not indexing into the structure at all, nor doing anything else that indicates an actual structure array. Mixing up and confusing data types, sizes, and arrangements is unlikely to make accessing your data very easy.
An actual structure array would probably simplify your data, then you could remove those nested cell arrays:
Apologies for the mix-up on Structure Array vs Cell Array, I'm still not as familiar with using these commands even after researching and reading around on the forums and help.
Why would it not still work all the same though? Just an additional step to reach and work with the data with which I'm working?
With what I have, in the Workspace I double-click on Structure_Array and it opens a 1x1 element like you said. I double-click on that and it opens a 1x4 array. Then each item in that 1x4 array is the 1000x10 table I'd like to worth with.
Taken into consideration what you suggested, how could I fill in each element within the Structure Array?
Looking at the link you sent, I want a 1x4 Structure, and each element in the structure will be that zeroed table.
So assuming I set up the structure array correctly, it would be something like 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
s(i).f(j,k) = Imported_Table(j,k)
else
end
end
end
end
So after the first pass-through of this script, it will be:
s(1).f(1,1)=Imported_Table(1,1)
In other words, in the first element of the structure (s), in the first row and first column, it will be the same number that is in the first row and first column of the Imported Table.
Then, while i=1, as j and k run through their for loop, it will populate every cell of the table in element 1 (s(1)) with the its corresponding number from the Imported Table.
Does my logic make sense?
"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:
"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.
Thank you for the reponse.
I ended up doing it the method you outlined in your first comment.
And the main issue was caused by me originally running the For Loop from 1 to length(Vector). But then I defined a variable such that Length_A = length(Vector). The issue was that I changed the word Vector to Length_A, but kept it as length(Length_A), so it was finding the length of a scalar, which is 1, which caused me to get the errors.
Bottom line, I have everything working as expected thanks to your help and my issue was my own lack of proofreading too detailed, I appreciate it.

Sign in to comment.

Answers (0)

Categories

Products

Release

R2021a

Asked:

Jon
on 7 Dec 2022

Commented:

Jon
on 7 Dec 2022

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!