Need to apply some tricky string interpolation for a for loop

6 visualizaciones (últimos 30 días)
Truman Cassady
Truman Cassady el 26 de Jun. de 2018
Editada: Stephen23 el 26 de Jun. de 2018
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 comentario
Stephen23
Stephen23 el 26 de Jun. de 2018
Editada: Stephen23 el 26 de Jun. de 2018
  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:

Iniciar sesión para comentar.

Respuestas (1)

OCDER
OCDER el 26 de Jun. de 2018
Editada: OCDER el 26 de Jun. de 2018
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

Categorías

Más información sobre Matrices and Arrays en Help Center y File Exchange.

Etiquetas

Community Treasure Hunt

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

Start Hunting!

Translated by