Extract variables from table and function with loop

조회 수: 2 (최근 30일)
wndud77
wndud77 2024년 4월 10일
댓글: Dyuman Joshi 2024년 4월 10일
Hi,
I have a table with 2317X16, which contains 16 variables of 2317X1. Name of each variable is MC1, MC2, MC3, MC4 ....
I would like to do a msbackadj function of each variable by using loop, then plot it separately.
(Name of another variable for X axis of msbackadj is 'Mshift', also size of 2317X1)
So I use:
for i = 1:16
Mc3 = msbackadj(Mshift,M3a(:,'i'));
Figure;plot(Mshift, M3a(:,'i'));
end
(Mc3 is random name I made for new variable)
And it just worked for 1 variable, but didn't go the loop.
How can I do msbackadj on each variable with using loop?
Thanks,
  댓글 수: 1
Dyuman Joshi
Dyuman Joshi 2024년 4월 10일
Using indexing (column numbers) instead of accessing by defining names manually would be a better option.
You can also get the variable names from the table and loop over them.

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

채택된 답변

Ayush Anand
Ayush Anand 2024년 4월 10일
Hi,
When you are using "M3a(:,'i')", MATLAB is interpreting 'i' as a literal character, not the variable 'i' from your loop. You can instead generate the column name separately using "sprintf" and then use parenthesis to index into the column:
colName = sprintf('MC%d', i);
% Extract the column for the current iteration
currentColumn = M3a.(colName);
% Perform msbackadj on the current column
Mc3 = msbackadj(Mshift, currentColumn);
Replacing the "Mc3 = msbackadj(Mshift,M3a(:,'i'));" line with the code above will ensure you are able to iterate through all columns of your data.
You can read more about sprintf here: https://www.mathworks.com/help/matlab/ref/sprintf.html

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Matrix Indexing에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by