46 views (last 30 days)
Show older comments
Leon on 18 Apr 2024 at 13:03
Edited: Leon on 21 Apr 2024 at 0:21
Accepted Answer: Steven Lord
Open in MATLAB Online
To iterate over the variables (columns) of a table, you can do this:
my_table = table([1; 2; 3], [4; 5; 6], ["Seven"; "Eight"; "Nine"])
my_table = 3x3 table
Var1 Var2 Var3 ____ ____ _______ 1 4 "Seven" 2 5 "Eight" 3 6 "Nine"
for var = my_table
disp(var)
end
Var1 ____ 1 2 3 Var2 ____ 4 5 6 Var3 _______ "Seven" "Eight" "Nine"
Is there a way to adjust this to operate on each row without using an index variable? Using a loop index variable, as follows, works fine but is a bit less elegant than the column-by-column solution.
for row_index = 1:height(my_table)
row = my_table(row_index, :);
disp(row)
end
Var1 Var2 Var3 ____ ____ _______ 1 4 "Seven" Var1 Var2 Var3 ____ ____ _______ 2 5 "Eight" Var1 Var2 Var3 ____ ____ ______ 3 6 "Nine"
2 Comments Show NoneHide None
Show NoneHide None
Torsten on 18 Apr 2024 at 13:57
Direct link to this comment
https://physical-modeling.mathworks.com/matlabcentral/answers/2108686-possible-to-iterate-over-table-rows-without-a-loop-index-variable#comment_3135996
"rows2vars" transposes your table, if it is that what you want to achieve.
Leon on 21 Apr 2024 at 0:12
Direct link to this comment
https://physical-modeling.mathworks.com/matlabcentral/answers/2108686-possible-to-iterate-over-table-rows-without-a-loop-index-variable#comment_3138496
Edited: Leon on 21 Apr 2024 at 0:18
Thanks. It's useful to know about but I want to get one table row each time, that I can index by the variable name, to be robust to changes in variable order and new variables in the future, whereas with rows2vars I would get a cell array (since the table variables are of different types) and have to index by number. Presumably it's also a bit inefficient converting to cells and using them, though that doesn't matter for my current use case since there is a lot of other processing per loop.
Sign in to comment.
Sign in to answer this question.
Accepted Answer
Steven Lord on 18 Apr 2024 at 15:33
To perform an operation on all rows of a table array you could use rowfun, but that isn't the same as writing a general for loop. I'd personally probably just use the for loop over 1:height(theTableArray).
2 Comments Show NoneHide None
Show NoneHide None
Bruno Luong on 18 Apr 2024 at 19:19
Direct link to this comment
https://physical-modeling.mathworks.com/matlabcentral/answers/2108686-possible-to-iterate-over-table-rows-without-a-loop-index-variable#comment_3136386
Edited: Bruno Luong on 18 Apr 2024 at 19:22
Some authority suggests using rowfun on table instead of for-loop row indexing if runtime performance matters.
https://www.mathworks.com/matlabcentral/discussions/general/847971-what-frustrates-you-about-matlab-2/2562386
Read the comments following my complain about combinations only providee table as output format.
Transpose the table as Torsen suggets or retreive the content of the table (without the tanle container) are two other work around of performance hi issue.
Leon on 21 Apr 2024 at 0:13
Direct link to this comment
https://physical-modeling.mathworks.com/matlabcentral/answers/2108686-possible-to-iterate-over-table-rows-without-a-loop-index-variable#comment_3138501
Edited: Leon on 21 Apr 2024 at 0:21
Interesting to know about for if I need more perfomance. I think I will just stick with having an index since I have a lot more processing in each loop, and want to be able to index by name for future-proofing if variables are added or order changed. Thanks
Sign in to comment.
More Answers (0)
Sign in to answer this question.
See Also
Categories
MATLABLanguage FundamentalsMatrices and ArraysMatrix Indexing
Find more on Matrix Indexing 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!
An Error Occurred
Unable to complete the action because of changes made to the page. Reload the page to see its updated state.
Select a Web Site
Choose a web site to get translated content where available and see local events and offers. Based on your location, we recommend that you select: .
You can also select a web site from the following list
Americas
- América Latina (Español)
- Canada (English)
- United States (English)
Europe
- Belgium (English)
- Denmark (English)
- Deutschland (Deutsch)
- España (Español)
- Finland (English)
- France (Français)
- Ireland (English)
- Italia (Italiano)
- Luxembourg (English)
- Netherlands (English)
- Norway (English)
- Österreich (Deutsch)
- Portugal (English)
- Sweden (English)
- Switzerland
- Deutsch
- English
- Français
- United Kingdom(English)
Asia Pacific
- Australia (English)
- India (English)
- New Zealand (English)
- 中国
- 日本Japanese (日本語)
- 한국Korean (한국어)
Contact your local office