How to apply a for loop in all tables with common indexing
Mostra commenti meno recenti
Hello,
I have a structure where multiple tables are combined. So I can access them by indexing.
I would like to apply a for loop in all the tables that have a common index.


I have more structures like the HYT, I would like to format all the tables that contain FLS in the indexing and all the tables that contain ULS.
So *.FLS. and *.ULS. the HYT. will be different for each structure but FLS and ULS will always be there as well as Float and LR inside each FLS and ULS.
So the variables in the workspace look like this:
HYT
ABC
DEF
then inside each of them we have both FLS and ULS with the Float and LR. I want to apply same actions and calculations to all tables inside FLS and all inside ULS for all the variables in the workspace.
Thank you very much in advance.
4 Commenti
"So the variables in the workspace look like this: HYT ABC DEF... I want to apply same actions ... for all the variables in the workspace."
And that is the start of your problems right there: accessing lots of separate variables is slow, complex, fragile, and inefficient.
In contrast, using one non-scalar structure would make this task trivially easy. Better data design makes your task very simple with basic, very efficient, and easy-to-debug indexing.
Possibly you painted yourself into this corner (i.e. bad data design) by forcing meta-data (e.g. "HYS") into variable names.
Don't. Do. That.
Meta-data is data, and data belongs in variables, not in their names. Store your meta-data as data in its own right, use one non-scalar structure, and then you can use normal, simple, very efficient indexing.
Usune Elizondo
il 27 Set 2021
S(1).name = 'HYS';
S(1).HLS = struct('FLS',..,'ULS',..):
S(1).ULS = struct(..);
S(2).name = 'ABC';
S(2).HLS = struct('FLS',..,'ULS',..):
S(2).ULS = struct(..);
S(3).name = 'DEF';
.. etc etc etc
This data is simple and efficient to loop over, you can use basic indexing and fieldnames to access all of the data. The meta-data are stored simply as under the "name" field, which you can trivially access:
(probably even better data design would be to "flatten" those structures so that they are not nested)
In contrast your poor data design requires slow, complex, and inefficient access (such as the code given in this answer):
Being "quite new" is not a problem, because that is the right time to learn how to write good MATLAB code!
Usune Elizondo
il 1 Ott 2021
Risposte (1)
Shanmukha Voggu
il 30 Set 2021
0 voti
Hi Usene,
Follow the steps in order to achieve the solution
2)Inside the previous loop start the second loop to iterate over FLS and ULS
3)Finally inside the second loop start the third loop to iterate over FLOAT and LR to make the calculations that are needed to be done on tables
4 Commenti
Usune Elizondo
il 4 Ott 2021
Stephen23
il 4 Ott 2021
"Now I have the data as you suggested. e.g. S(1).FLS has 3 tables inside, how can I apply a for loop to all tables inside S(1).FLS."
Based on what you have explained so far and making a few guesses, perhaps something like this:
for ii = 1:numel(S)
C = fieldnames(S(ii).FLS);
for jj = 1:numel(C)
T = S(ii).(C{jj})
.. do whatever with table T
end
end
Usune Elizondo
il 4 Ott 2021
Categorie
Scopri di più su Loops and Conditional Statements in Centro assistenza e File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!