0

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.

1
  • 1
    I 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 to Commented Aug 4, 2015 at 12:34

1 Answer 1

3

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...
Sign up to request clarification or add additional context in comments.

3 Comments

Why not just vars = whos('Xaws*')? What does 'name' mean here?
@LuisMendo I didn't know you could do that. the 'name' just extracts the name field as a cell array... edited to use * instead
@Dan Weird. whos('name') doesn't work for me (it just looks for variables called 'name')

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.