How to resolve the error "cell contents reference from a non-cell array object"? Thank you!!

2 views (last 30 days)
Looking for help on my project. Any help is largely appraciated!!
fid = fopen('test.cal');
[paramlist l] = textscan(fid, '%s %s %s %s %s');
fclose(fid);
[len wid] = size(paramlist{1}); %[len wid]=[18 1]
j = 1;
chanlist = cell(1,len); %create a cell array named chanlist
for i=1:len
if(strcmp(paramlist{1}{i},'channel'));
chanlist{1,j} = {paramlist{2}{i},paramlist{3}{i},paramlist{4}{i},paramlist{5}{i}};
disp(chanlist{1,j});
j=j+1;
end
end
for i=1:len
if(strcmp(chanlist{i}{1}, 'DIGOUT'))%%when I run the code, it shows error happens here
if (str2double(chanlist{i}{3}) < 28) %28 digital IOs on a minilab1008
if (strcmp(chanlist{i}{2}, '0'))
disp('board 0 DIGOUT');
elseif (strcmp(chanlist{i}{2},'1'))
disp('board 1 DIGOUT');
else 'only boards 0 and 1 supported';
end
else 'ignoring digital output';
end
elseif strcmp(chanlist{i}{1}, 'DIGIN')
if (str2double(chanlist{i}{3}) < 28) %28 digital IOs on a minilab1008
if (strcmp(chanlist{i}{2}, '0'))
disp('board 0 DIGIN');
elseif (strcmp(chanlist{i}{2},'1'))
disp('board 1 DIGIN');
else 'only boards 0 and 1 supported';
end;
else 'ignoring digital output';
end;
end
end
Here is the "test.cal" file: (3rd column represents board number, 4th column represents line/channel number)
channel DIGOUT 0 0 shutter1
channel DIGOUT 0 1 shutter2
channel DIGOUT 0 2 shutter3
channel DIGOUT 0 3 shutter4
channel DIGOUT 1 0 shutter5
channel DIGIN 0 4 shutter1state
channel DIGIN 0 5 shutter2state
channel DIGIN 0 6 shutter3state
channel DIGIN 0 7 shutter4state
channel DIGIN 1 1 shutter5state
channel ANAOUT 0 0 TempCtrlVolt
channel ANAOUT 0 1 PositionCtrlVolt
channel ANAOUT 1 0 TempCtrlVolt
channel ANAIN 0 0 PSUVolt
channel ANAIN 0 1 PositionCtrlState
channel ANAIN 1 0 PSUVolt
calib TempCtrlVolt 0 120
calib TempCtrlVolt 1 120

Accepted Answer

Jan
Jan on 4 Dec 2011
Check the type of chanlist{i} by setting either a breakpoint in the failing line or let the debugger stop Matlab in case of errors:
dbstop if error
The next problem will occur, when you check chanlist{i}{3} < 28: You've imported all data using the '%s' format in textscan. Therefore you cannot find numeric data in the cells.
  3 Comments
Jan
Jan on 5 Dec 2011
Please check the type of "chanlist{i}". If "chanlist{i}{1}" fails with the message, that it is not a cell object, this expression muist be adjusted to the type.
I think this line is too complicated and the level of cell nesting got out of control:
chanlist{1,j} = {paramlist{2}{i},paramlist{3}{i},paramlist{4}{i},paramlist{5}{i}}
You can simplify this until it is clear, what the type of chanlist{i} is.
Jan
Jan on 6 Dec 2011
Please, Wang, give us a chance. Use "dbstop if error", check the type of "chanlist{i}" and post the results. We want help, but you do not post the necessary details. And the problem is really easy. But it would be useless if I solve it here completely and you do not understand the underlying problem - such that same error will catch you a few lines later again.

Sign in to comment.

More Answers (1)

Fangjun Jiang
Fangjun Jiang on 7 Dec 2011
You have 18 lines in the text file. The variable "paramlist" got the correct value after reading the file. The variable "chanlist" is set as 1x18 cells. The value of "len" is 18 which is also correct.
However, in your first for-loop, because you have the comparison to check for the string of 'channel', only the first 16 cells of "chanlist" are assigned correct value. The 17th and 18th cells are empty. That is why it causes error when you do the second for-loop with 18 iterations.
If you change the second for-loop as below, it will run without error. However, I am not sure if that is what you want.
for i=1:j-1 %len
Jan's recommendation of using "dbstop if error" is really a good suggestion. You can also try to run the code step by step. But because the error happened at the later part of the for-loop. "dbstop if error" is truly the best to pin-point the error.

Categories

Find more on Software Development Tools 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!