Possible to iterate over table rows without a loop index variable? (2024)

46 views (last 30 days)

Show older comments

Leon on 18 Apr 2024 at 13:03

  • Link

    Direct link to this question

    https://physical-modeling.mathworks.com/matlabcentral/answers/2108686-possible-to-iterate-over-table-rows-without-a-loop-index-variable

  • Link

    Direct link to this question

    https://physical-modeling.mathworks.com/matlabcentral/answers/2108686-possible-to-iterate-over-table-rows-without-a-loop-index-variable

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.

2 Comments

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

  • Link

    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

  • Link

    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

  • Link

    Direct link to this answer

    https://physical-modeling.mathworks.com/matlabcentral/answers/2108686-possible-to-iterate-over-table-rows-without-a-loop-index-variable#answer_1443946

  • Link

    Direct link to this answer

    https://physical-modeling.mathworks.com/matlabcentral/answers/2108686-possible-to-iterate-over-table-rows-without-a-loop-index-variable#answer_1443946

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

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

  • Link

    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

  • Link

    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

Tags

  • table
  • rows
  • iterate

Products

  • MATLAB

Release

R2023b

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.


Possible to iterate over table rows without a loop index variable? (7)

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

Possible to iterate over table rows without a loop index variable? (2024)
Top Articles
Latest Posts
Article information

Author: Edmund Hettinger DC

Last Updated:

Views: 6302

Rating: 4.8 / 5 (78 voted)

Reviews: 93% of readers found this page helpful

Author information

Name: Edmund Hettinger DC

Birthday: 1994-08-17

Address: 2033 Gerhold Pine, Port Jocelyn, VA 12101-5654

Phone: +8524399971620

Job: Central Manufacturing Supervisor

Hobby: Jogging, Metalworking, Tai chi, Shopping, Puzzles, Rock climbing, Crocheting

Introduction: My name is Edmund Hettinger DC, I am a adventurous, colorful, gifted, determined, precious, open, colorful person who loves writing and wants to share my knowledge and understanding with you.