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

4 Ansichten (letzte 30 Tage)
Youngjin
Youngjin am 24 Jan. 2024
Bearbeitet: Stephen23 am 24 Jan. 2024
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 Kommentar
Stephen23
Stephen23 am 24 Jan. 2024
Bearbeitet: Stephen23 am 24 Jan. 2024
"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.

Melden Sie sich an, um zu kommentieren.

Akzeptierte Antwort

Angelo Yeo
Angelo Yeo am 24 Jan. 2024
Bearbeitet: Angelo Yeo am 24 Jan. 2024
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

Weitere Antworten (1)

Matt J
Matt J am 24 Jan. 2024
Bearbeitet: Matt J am 24 Jan. 2024
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 Kommentare
Matt J
Matt J am 24 Jan. 2024
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 am 24 Jan. 2024
@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?

Melden Sie sich an, um zu kommentieren.

Produkte

Community Treasure Hunt

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

Start Hunting!

Translated by