How can I use a loop to store a table in separate arrays?

조회 수: 1 (최근 30일)
Juliette Smoorenburg
Juliette Smoorenburg 2020년 3월 24일
댓글: Obinna Princewill Okoro 2020년 3월 24일
Hi all,
I've got a table [35040x29 table] and I want to create separate arrays so I'll get a [35040x1] format
In the following code I did it manually, but I want to use a loop to create the separate arrays in a much faster way.
house_1_U = table1.H01U_kWh;
house_1_G = table1.H01G_kWh;
house_2_U = table1.H02G_kWh;
house_2_G = table1.H02U_kWh;
Thanks in advance!
Juliette
  댓글 수: 2
Obinna Princewill Okoro
Obinna Princewill Okoro 2020년 3월 24일
initial = 35040 for i = 1:29 y = initial * i end
Obinna Princewill Okoro
Obinna Princewill Okoro 2020년 3월 24일
initial = 35040 for i = 1:29 y = initial * i end

댓글을 달려면 로그인하십시오.

채택된 답변

Adam Danz
Adam Danz 2020년 3월 24일
편집: Adam Danz 2020년 3월 24일
The best approach is to not break apart the table and use indexing instead. Tables are neat, tidy, and they keep the data together rather than scattering the data between several variables. Instead of using house_1_G you could just use table1.H01G_kWh.
If you must break apart the table, you could put each column into a cell array using the line below.
c = arrayfun(@(x){table1(:,x)}, 1:size(table1,2));
Then, to access column 2,
c{2}
Avoid putting each column of the table into a separate variables as the demo shows in your question. This requires dynamic variable naming which is a really bad form of programming and causes many problems.
  댓글 수: 1
Juliette Smoorenburg
Juliette Smoorenburg 2020년 3월 24일
Thank you so much for the fast response! I get it know, thanks a lot!

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Matrices and Arrays에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by