how to solve "Subscripted assignment dimension mismatch" error?

4 views (last 30 days)
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.
  3 Comments
Walter Roberson
Walter Roberson on 29 Nov 2011
It turns out not to matter if there are blank lines if you are using %s format: %s skips leading whitespace (just like the number formats do), so textscan() would skip over the blank lines.
Jan
Jan on 29 Nov 2011
@Walter: Correct. I just wanted to be sure, that we are talking about the same problem.
@Yu Wang: The problem gets obvious, if you try to run your program line by line. Most of all running "[paramlist{2}{1},paramlist{3}{1},paramlist{4}{1},paramlist{5}{1}]" in the command window reveals the error.
It is a good idea to perform some debugging, when a problem occurs...

Sign in to comment.

Accepted Answer

Walter Roberson
Walter Roberson on 29 Nov 2011
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);
  2 Comments
Yu Wang
Yu Wang on 29 Nov 2011
Thank you very much. In fact what I am trying to do is write all the info of "test.cal" file into a single variable so that I could use the info of it when this m-file is called by another m-file (im a totally a rookie, I guess). It could be anything, and in this case, I used matrix of zeroes which seems not be a wise choice. Could you give me any advice on this? Thank you very much.
Walter Roberson
Walter Roberson on 29 Nov 2011
chanlist = paramlist(:,2:end);
copies all of the information except the "channel" word.
What format does the second m-file expect to receive the data in?
Do each of the string fields have a limited number of possible values? If so then do you want to encode the string for that field as a numeric value that represents which of the strings was read? If so then the file contents could plausibly be encoded to a numeric matrix.
For example,
[tf, caldirs] = ismember(paramlist{2}, {'DIGIN', 'DIGOUT'});
calfield3 = str2double(paramlist{3});
calfield4 = str2double(paramlist{4});
[tf, calshut] = ismember(paramlist{5}, {'shutter1', 'shutter2', 'shutter3', 'shutter4', 'shutter5'});
chanlist = [caldirs(:), calfield3(:), calfield4(:), calshut(:)];
and then chanlist would be a something-by-4 numeric matrix, with 1 in the first column meaning 'DIGIN', 2 there indicating 'DIGOUT', and so on.

Sign in to comment.

More Answers (0)

Community Treasure Hunt

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

Start Hunting!