0

Suppose I get directory list with

files = dir(mypattern);

Now I have 10000x1 struct in files.

If I do

filenames = {files.name};

I will have 1x10000 cell in filenames.

I.e. with transposed orientation.

How to get 10000x1 cell from files in one step here?

1
  • 1
    why is it so problematic? Commented Feb 22, 2016 at 11:25

3 Answers 3

3

How about

filenames = {files.name}.';
Sign up to request clarification or add additional context in comments.

Comments

2

Just to explain what is happening. files.name is a Comma-Separated List which behaves like {files(1).name,files(2).name,files(3).name} (simplifying to three elements) because it is separated by comma, not by a semicolon. There is no Semicolon-Separated List or similar, you have to transpose or reshape to get the dimensions you want.

For vectors the solution from Shai is perfect, reshape is a simple solution which also applies to more dimensional data structures:

files=struct('names',{'a','b';'c','d'}); %example data 2x2
reshape({files.names},size(files));

Comments

0

An alternative way is:

filenames = arrayfun(@(x) x.name, dir(mypattern), 'uni',0);

Comments

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.