How can i select variables which has same number at the end

조회 수: 1 (최근 30일)
Youngjin
Youngjin 2024년 1월 24일
편집: Stephen23 2024년 1월 24일
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
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
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
Your variables are: T_Right_F_100 i_pat myDigitsPats res_100 str time_100 varNames T_Right_F_2500 idxWithDigits pat res_2500 time time_2500

추가 답변 (1개)

Matt J
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
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
Times = 1×3 cell array
{[10 20]} {[30 40]} {[50 60]}
Adam Danz
Adam Danz 2024년 1월 24일
@Youngjin how are you accessing the data? Are you loading a mat file that was given to you or are you loading the data from another source?

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

카테고리

Help CenterFile 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!

Translated by