Borrar filtros
Borrar filtros

Propagate Data Within Structure Array Based on Pre-Existing Table

1 visualización (últimos 30 días)
Jon
Jon el 7 de Dic. de 2022
Comentada: Jon el 7 de Dic. de 2022
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 comentarios
Stephen23
Stephen23 el 7 de Dic. de 2022
Editada: Stephen23 el 7 de Dic. de 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:
"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.
Jon
Jon el 7 de Dic. de 2022
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.

Iniciar sesión para comentar.

Respuestas (0)

Categorías

Más información sobre Loops and Conditional Statements en Help Center y File Exchange.

Productos


Versión

R2021a

Community Treasure Hunt

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

Start Hunting!

Translated by