loop over variable with different characters

Hi,
I wonder if someone cna help me with my problem and that is if I have lets say 4 variables in my workspace with different
characters in the begnning end ends with the same words, for example:
abcd_hpcd_Df
defg_hpcd_Df
hijk_hpcd_Df
lmno_hpcd_Df
I want to write it in matlab in a easier way, just write hpcd_Df and not the characters in the beginning.

5 Comments

Don't name the variables like that. Use the convention you want.
Sure, this is just an example, but the real convention is as following:
ijjk_hpcd_Df
byzu_hpcd_Df
hhet_hpcd_Df
vffr_hpcd_Df
its just the 4 words in the beginning that are different, and I want to point out also that one of them are only once being
included in the workspace. To call it I want to use _hpcd_Df and not to check what words it begins with every time.
What do you want to do with the variables?
I want to call them and insert them into a table as a column with these variables. The thing is
that each of the variables is among 100 other variables and I dont know what words they begin with
and to look visually and find each is a very time consuming. Therefore when I clean the workspace and insert
new 100 variables I want to write like_hpcd_Df to find the corresponding variable.

Sign in to comment.

 Accepted Answer

This shows, that you have created a bunch of variables and stored important information in the name of the variables. This design is a typical DON'T in programming.
But is you do not want to re-design the code from scratch, this might help:
clear variables
abcd_hpcd_Df = 1;
defg_hpcd_Df = 1;
hijk_hpcd_Df = 1;
lmno_hpcd_Df = 1;
abcde = 2;
VarList = whos;
NameList = {VarList.name};
NameList(endsWith(NameList, '_hpcd_Df'))
ans = 1×4 cell array
{'abcd_hpcd_Df'} {'defg_hpcd_Df'} {'hijk_hpcd_Df'} {'lmno_hpcd_Df'}

4 Comments

Alright this is the solution that I want, but I have now another problem from this and that is how
can I access the data from the tables?. Lets say If I want to show the data for each variable in a table.
This would be much easier, if you decide for another design, which does not populate the workspace with many variables, but store them in a struct. eval is a shot in your knee. TUTORIAL: Why and how to avoid Eval
Sigh.
abcd_hpcd_Df = 1;
defg_hpcd_Df = 1;
hijk_hpcd_Df = 1;
lmno_hpcd_Df = 1;
abcde = 2;
VarList = whos;
NameList = {VarList.name};
NameList = NameList(endsWith(NameList, '_hpcd_Df'));
T = table();
for k = 1:numel(NameList)
T.(NameList{k}) = eval(NameList{k}); % BAD, don't blame me
end
T
T = 1×4 table
abcd_hpcd_Df defg_hpcd_Df hijk_hpcd_Df lmno_hpcd_Df ____________ ____________ ____________ ____________ 1 1 1 1
This is the exact answer to my question. Thanks alot!
abcd_hpcd_Df = 1;
defg_hpcd_Df = 1;
hijk_hpcd_Df = 1;
lmno_hpcd_Df = 1;
abcde = 2;
save('mydata.mat','-regexp','_hpcd_Df$')
S = load('mydata.mat')
S = struct with fields:
abcd_hpcd_Df: 1 defg_hpcd_Df: 1 hijk_hpcd_Df: 1 lmno_hpcd_Df: 1

Sign in to comment.

More Answers (0)

Asked:

on 29 Sep 2022

Commented:

on 2 Oct 2022

Community Treasure Hunt

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

Start Hunting!