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

2 vues (au cours des 30 derniers jours)
Youngjin
Youngjin le 24 Jan 2024
Modifié(e) : Stephen23 le 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 commentaire
Stephen23
Stephen23 le 24 Jan 2024
Modifié(e) : Stephen23 le 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.

Connectez-vous pour commenter.

Réponse acceptée

Angelo Yeo
Angelo Yeo le 24 Jan 2024
Modifié(e) : Angelo Yeo le 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

Plus de réponses (1)

Matt J
Matt J le 24 Jan 2024
Modifié(e) : Matt J le 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 commentaires
Matt J
Matt J le 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 le 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?

Connectez-vous pour commenter.

Catégories

En savoir plus sur Workspace Variables and MAT Files dans Help Center et File Exchange

Produits

Community Treasure Hunt

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

Start Hunting!

Translated by