How to store strings into array?

I am reading filename from directory and want to store it into array.
srcFiles = dir('E:\abc\*.bmp'); % the folder in which ur images exists
for i = 1 : length(srcFiles)
filename = strcat('E:\abc\',srcFiles(i).name);
names(i,:)=filename;
end
I am getting following error ??? Undefined function or variable 'names'.

3 commentaires

Stephen23
Stephen23 le 26 Août 2016
Modifié(e) : Stephen23 le 26 Août 2016
Why waste your time with a loop anyway ?
P = 'E:\abc\';
S = dir(fullfile(P,'*.bmp');
N = {S.name};
F = fullfile(P,N)
You don't need cellfun
F=fullfile(P,N)
Stephen23
Stephen23 le 26 Août 2016
@Azzi Abdelmalek: thank you, I changed the comment.

Connectez-vous pour commenter.

 Réponse acceptée

Adam
Adam le 26 Août 2016

0 votes

I wouldn't expect you to be getting that specific error, but strings need to be stored in a cell array, not a numeric array generally:
names{i} = filename;
You may want to presize names though as
names = cell.empty( length(srcFiles), 0 );
or something similar.

5 commentaires

anu
anu le 27 Août 2016
Modifié(e) : anu le 27 Août 2016
But how to access names array. I tried >> names It shows me Columns 1 through 6
[1x33 char] [1x33 char] [1x33 char] [1x33 char] [1x33 char] [1x32 char]
Columns 7 through 10
[1x32 char] [1x32 char] [1x32 char] [1x32 char]
How to access names array. Also how to store all 100 names on separate row in excel sheet.
names{1}
names{2}
anu
anu le 27 Août 2016
Thanks a lot. But when I stored names in excel sheet xlswrite('c:\data.xls',names,'sheet1') it stores name in single row in different columns. But I want the names in different row but same column. How to do it?
I think the reason is obvious, your cell array is a row vector, then you have just to transpose it
names=names'
anu
anu le 29 Août 2016
Thanks a lot.

Connectez-vous pour commenter.

Plus de réponses (1)

Catégories

En savoir plus sur Characters and Strings dans Centre d'aide et File Exchange

Question posée :

anu
le 26 Août 2016

Commenté :

anu
le 29 Août 2016

Community Treasure Hunt

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

Start Hunting!

Translated by