MATLAB: How to solve “Subscripted assignment dimension mismatch” error

subscripted assignment dimension mismatch

Hi everyone, any help on this will be highly appreciated!!
Here is my program:
fid = fopen('test.cal');
[paramlist l] = textscan(fid, '%s %s %s %s %s'); %paramlist is a variable
fclose(fid);
[len wid] = size(paramlist{1}); %len=3, wid=4
chanlist = zeros(3,4); %chanlist is a variable
chanlist(1,1:4) = [paramlist{2}{1},paramlist{3}{1},paramlist{4}{1},paramlist{5}{1}]; %write the info from the test.cal into the 1st row of matrix, chanlist. Error happen due to this line.
Here is the "test.cal" file:
channel DIGOUT 0 0 shutter1
channel DIGOUT 0 1 shutter2
channel DIGOUT 0 2 shutter3
When I run the program, the error "subscripted assignment dimension mismatch" will show up, I really dont know how resolve it.

Best Answer

chanlist = zeros(3,4); defines chanlist to be a numeric variable.
paramlist{2}{1} extracts the string from paramlist{2}{1}, which should be 'DIGOUT' . Strings in MATLAB are row vectors of characters.
[paramlist{2}{1},paramlist{3}{1},paramlist{4}{1},paramlist{5}{1}] likewise extracts all the strings from remaining cells. So at this point you are constructing
['DIGOUT','0','0','shutter1']
The result of applying the [] list-building operator to a series of row vectors of characters, is a row vector containing all of the characters, so this expression is going to construct
'DIGOUT00shutter1'
and then you attempt to assign these 16 characters in to the 4 numeric locations chanlist(1,1:4) . Assigning characters to numeric locations is well-defined, with each character corresponding to the value of the character's ordinal number (e.g., '0' happens to be ordinal 48, char(48) == '0'), so the character to numeric assignment is merely going to startle you rather than crash your code -- but you need as many storage locations as you have characters.
It is difficult to tell what you want to do, but I suspect it would be
chanlist = cell(size(paramlist) - [0 1]);
for K = 1:size(paramlist,1)
chanlist(K,:) = paramlist(K,2:end);
end
Which could be done more simply as
chanlist = paramlist(:,2:end);
Related Question