Error using AppDesigner attempting to output to a TextArea

Hello,
I am trying to build an app that automates a decision tree to output a patient treatment recommendation based on data in an Excel spreadsheet.
I would greatly appreciate any help I can get to address the following error on the code below:
Error using matlab.ui.control.TextArea/set.Value
'Value' must be a character vector, or a 1-D array of the following type: cell array of character vectors, string, or categorical.
Error in FreemanMillerKneeTreatmentDecisionTreeApp2/FreemanMillerFootDecisionTreeButtonPushed (line 1189)
app.OutputTextArea.Value{i+1} = recommendation; %this writes each rec to a new row in the 'Output' window
Error in matlab.apps.AppBase>@(source,event)executeCallback(ams,app,callback,requiresEventData,event) (line 62)
newCallback = @(source, event)executeCallback(ams, ...
Error while evaluating Button PrivateButtonPushedFcn.
c = ['Patient: ', mrn, 'has a diagnosis of ', diagnosis, '. ', trial,' ', side, ' Side Output: ', warning, recommendation, ' ']; %this creates a string array
ca = cellstr(c); %this converts the string array to a cell array
rec = join(ca); % join the cell array of strings into one string
app.OutputTextArea.Value{i+1} = rec; %this writes each rec to a new row in the 'Output' window

4 Comments

You should put a breakpoint in this line of code (1189). Run your app and when the app stop, use the Matlab prompt to check the class of the data you are trying to put on this TextArea object.
>> class(MyDataThatIAmTryingToPutInTheObject)
It looks like you are trying to set a value that its class is different than "character vector, or a 1-D array of the following type: cell array of character vectors, string, or categorical".
Thank you for the suggestion
I used:
className = class(rec);
disp(class);
The output in the prompt was:
cell
Here is a more complete look at the code:
% loop through each row of data in Excel spreadsheet
for i = 1:app.rows
diagnosis=string(app.data.PrimaryDiagnosis(i));
patternofinvolvement = string(app.data.PatternOfInvolvement(i));
mrn = string(app.data.MRN(i));
side = string(app.data.MotionParams_Side(i));%set the side variable to Left or Right string
trial = string(app.data.GcdFile(i));
recommendation = 'Surgery';
c = ['Patient: ', mrn, 'has a diagnosis of ', diagnosis, '. ', trial,' ', side, ' Side Output: ', recommendation, ' \n']; %this creates a string array
ca = cellstr(c); %this converts the string array to a cell array
rec = join(ca); % join the cell array of strings into one string
%disp(c); display c in the prompt
className = class(rec);
disp(className)
app.OutputTextArea.Value{i+1} = rec; %this writes each rec to a new row in the 'Output' window
end %End of For loop to go through all of the rows in the spreadsheet
As a style note, I do not recommend using [] with '' literals and variables to create something that must specifically be a string array. What if the variables turned out to be character vectors in some flow paths? You are more robust to use "" strings as then the variables will be converted to string if needed, and the people reading your code do not need to go back and verify all flow paths to be sure that in every case at least one of the variables would be string.

Sign in to comment.

Answers (2)

app.OutputTextArea.Value{i+1} = rec;
your rec is a cell, and the destination is the contents of a cell. The Value would not end up as a cell of character vectors, it would end up as a cell of cells of character vectors.
@Adam Graf, you don't really need this loop. Try this...
mnr = {'Eric'; 'Marina'};
diagnosis = {'Lowback Pain'; 'Headache'};
recommendation = {'Meditation'; 'Tylenol'};
data = table(mnr, diagnosis, recommendation)
data = 2×3 table
mnr diagnosis recommendation __________ ________________ ______________ {'Eric' } {'Lowback Pain'} {'Meditation'} {'Marina'} {'Headache' } {'Tylenol' }
%% Instead of:
% c = ['Patient: ', mrn, 'has a diagnosis of ', diagnosis, '. ', trial,' ', side, ' Side Output: ', recommendation, ' \n'];
% ca = cellstr(c);
% rec = join(ca);
% app.OutputTextArea.Value{i+1} = rec;
%% Try this:
rec = "Patient: " + data.mnr + " has a diagnosis of " + data.diagnosis + ". Side Output: " + data.recommendation
rec = 2×1 string array
"Patient: Eric has a diagnosis of Lowback Pain. Side Output: Meditation" "Patient: Marina has a diagnosis of Headache. Side Output: Tylenol"
app.TextArea.Value = rec;

Products

Release

R2022b

Asked:

on 6 Mar 2023

Answered:

on 7 Mar 2023

Community Treasure Hunt

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

Start Hunting!