Subscripted assignment dimension mismatch.

2 ビュー (過去 30 日間)
JKM1000 2016 年 2 月 1 日
編集済み: Guillaume 2016 年 2 月 1 日
I really don't understand why I get this error. I'm trying to make a new array that contains the strings from 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 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 = {}


Find more on Characters and Strings in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by