I would like to store the names of some variables in my workspace into a string array given a prefix. In particular, I have 'Xaws1', 'Xaws2', 'Xaws3' variables other than others in my workspace, and I want their names to populate an array which would look something like {'Xaws1', 'Xaws2', 'Xaws3'}.
In my case all the variable names have the same length (5 characters), and I'd like to find them using a prefix like "Xaws*". I tried with who('Xaws*), but the output seems not usable in this sense. I also looked into this post, but could not find a solution so far.
Any idea would be very appreciated, thanks in advance.
-
1I know this is the opposite of what you are asking, but it probably still applies: stackoverflow.com/questions/16099398/… your variables should probably rather be a cell array... have a look at the link in the comments of the question I linked toDan– Dan2015-08-04 12:34:42 +00:00Commented Aug 4, 2015 at 12:34
Add a comment
|
1 Answer
You can do this using whos('Xaws*'), you just need to extract the name field afterwards:
vars = whos('Xaws*');
names = {vars.name}
However, I strongly suggest that you don't have a bunch of variables named Xaws? where ? is incrementing numbers. Rather use a cell array such that
Xaws{1} = Xaws1;
Xaws{2} = Xaws2;
etc...
3 Comments
Luis Mendo
Why not just
vars = whos('Xaws*')? What does 'name' mean here?Dan
@LuisMendo I didn't know you could do that. the
'name' just extracts the name field as a cell array... edited to use * insteadLuis Mendo
@Dan Weird.
whos('name') doesn't work for me (it just looks for variables called 'name')