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.
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/1598256/image.png)
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
参考
製品
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!