1
votes

I cant figure out how to print a cell array in a static text box. I am trying to check three numerical values in an array. If the values are < 3 then I store the string into a cell array called warning. I want to display all the values of the cell array 'warning' into a static text box in matlab gui. I am having trouble with the last few lines. Not sure what's going on. Any help is appreciated. I am beginner level; please something I can understand.

 arr=[mathtotal englishtotal sciencetotal]  %three numerical values
 warning={};  % storing the message that corresponds to the values.
 for x=arr
   if x<3.0
      warning={warning 'warning your total is less than 3'};
   else 
      warning={warning ''}  % do nothing if the value is not less than 3.
    end
      end
% done gathering the messages. now trying to print.  having trouble here.

 for x=1:3

      str=sprintf('%s ', warning{x})  % trying to iterate into a variable
 end

 set(handles.text8, 'String', str)   % trying to print the warning but not working......
2

2 Answers

0
votes

"Not working" is a not really helpful error description.

Just guessing...

In each iteration of

for x=1:3
      str=sprintf('%s ', warning{x})  % trying to iterate into a variable
end

you're replacing the value of str - so in the end it will only contain the value of the last iteration. You could use:

str = sprintf('%s ', warning{:});

sprintf repeats the format string until all input arguments are consumed.

0
votes

This should help:

warning={};  % storing the message that corresponds to the values.
 for x=arr
     if x<3.0
      warning(end+1)={'warning your total is less than 3'};
     else 
      warning(end+1)= {''};  % do nothing if the value is not less than 3.
      end
 end

if you want to know more about cells-> TMW: cell-arrays