Subscripted assignment dimension mismatch.

1 次查看(过去 30 天)
I really don't understand why I get this error. I'm trying to make a new array that contains the strings from filelist.name so that I can then get rid of the .suffix of my filelist for future manipulations. However, I get the "subscripted assignment dimension mismatch" error when I try to create newfilelist. The error is with the last line of code shown. Up to that point filelist(i).name happily returns the name of the ith file. I'm pretty new to coding so I appreciate any help.
Direc= 'somedirectory';
fileprefix = 'someprefix';
filelist = dir([Direc '/' someprefix '_*']);
newfilelist(1:length(filelist)) = filelist(1:length(filelist)).name;

回答(1 个)

Guillaume
Guillaume 2016-2-1
编辑:Guillaume 2016-2-1
First, you should use fullfile to build paths rather than building them yourself.
filelist = dir(fullfile(Direc, [someprefix, '_*']));
Secondly, I would use numel instead of length since length behaves very differently with matrices. But in any case, x(1:length(x)) when x is the vector is the same as x, so the whole construct is pointless.
When you ask for a single field of a structure array, matlab returns a comma separated list. If you want to convert that into a single entity, you need to concatenate that list. As the elements of the list are strings, you need to concatenate them into a cell array, thus:
newfilelist = {filelist.name}

类别

Help CenterFile Exchange 中查找有关 Structures 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by