Need to apply some tricky string interpolation for a for loop

조회 수: 6 (최근 30일)
Truman Cassady
Truman Cassady 2018년 6월 26일
편집: Stephen23 2018년 6월 26일
I would like to interpolate for several individual arrays. "T5_11" is one 1x24 array that I have made, and "T5_10" is another completely separate array I have made. I want the interp function to access each T5_k array from the for loop that changes the last two digits of the T5 array. How can I do a string interpolation that will change an iterator that changes the reference name of the array I'm working with? -Thanks
  댓글 수: 1
Stephen23
Stephen23 2018년 6월 26일
편집: Stephen23 2018년 6월 26일
  1. Normal MATLAB does not have a function interp.
  2. If you put all of your data into one array arr where each column is one data set, then this would be trivial with just one interp1 call.
  3. dynamically accessing variable names is one way that beginners force themselves into writing slow, complex, buggy code that is hard to debug. Read this to know why:

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

답변 (1개)

OCDER
OCDER 2018년 6월 26일
편집: OCDER 2018년 6월 26일
Read the following about why labeling variables "T5_11" and "T5_10" is extremely difficult to work with. https://www.mathworks.com/matlabcentral/answers/304528-tutorial-why-variables-should-not-be-named-dynamically-eval
To fix, you need to go back to your code and replace T5_k with T5{k}, where k is an integer. Then you can do what you want to do easily by summoning T5{k} instead of ["T5_" num2str(k)] (which does NOT work by the way).
EXAMPLE
T5_1 = [1 2 3 4 5] REPLACE CODE WITH ==> T5{1} = [1 2 3 4 5];
T5_1 = [6 7 8 9 10] REPLACE CODE WITH ==> T5{2} = [6 7 8 9 10];
...
% You'll have a cell array of matrix called T5 of size 1x24
for k = 1:24
interp(timeOfDay,linspace(60,90,24), T5{k});
end

카테고리

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