How can i select variables which has same number at the end
조회 수: 1 (최근 30일)
이전 댓글 표시
I want to create a code that selects variables with the same number at the end and interpolates them.
where,
there are T_Right_F_224 and time_224 and time is 0:0.01:100
i can make interpolation with interp1(time,T_Right_F_224,time_224)
but,
i want make automation code that select variables with same number at the end
please help.

댓글 수: 1
Stephen23
2024년 1월 24일
편집: Stephen23
2024년 1월 24일
"but these variables are from INCA data which comes from MDF file directly which i didnt meant to name this way....."
You did not tell us the most important information: how did you get those variables into the workspace?
Where these variables are imported would be the best place to fix this bad data design.
채택된 답변
Angelo Yeo
2024년 1월 24일
편집: Angelo Yeo
2024년 1월 24일
You can combine string pattern search and eval in order to accomplish what you want. However, in general, it is not recommended to use eval with dynamically named variables. It would be difficult to debug in a near future.
clear;
time_2500= transpose(linspace(0,100, 2500));
T_Right_F_2500 = rand(2500,1);
time_100 = transpose(linspace(0,100, 100));
T_Right_F_100 = rand(100,1);
time = 0:0.01:100;
% Let's see what variables are in workspace
varNames = string(who);
% Let's extract which digits patterns there are
% The pattern is "time_" plus something.
pat = "time_"+digitsPattern;
idxWithDigits = contains(varNames, pat);
myDigitsPats = unique(extract(varNames(idxWithDigits), pat));
myDigitsPats = extractAfter(myDigitsPats, "_");
for i_pat = 1:length(myDigitsPats)
% What to evaluate:
% res_100 = interp1(time_100, T_Right_F_100, time);
str = "res_"+myDigitsPats(i_pat)+ "= interp1(time_" + myDigitsPats(i_pat)+", T_Right_F_"+myDigitsPats(i_pat)+", time);";
eval(str)
end
who
댓글 수: 0
추가 답변 (1개)
Matt J
2024년 1월 24일
편집: Matt J
2024년 1월 24일
You shouldn't have variables named that way. You should have a cell array T_Right_F{i}, and times{i} and should use indexing to get the corresponding ones,
interp1(time,T_Right_F{224},times{224})
See alos,
댓글 수: 3
Matt J
2024년 1월 24일
Then you should undo that damage before proceeding with further work. E.g.,
time_1=[10,20];
time_2=[30,40];
time_3=[50,60];
Times=cell(1,3);
for i=1:numel(Times)
Times{i}=eval("time_"+i);
end
Times
참고 항목
카테고리
Help Center 및 File Exchange에서 Workspace Variables and MAT Files에 대해 자세히 알아보기
제품
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!