Error using AppDesigner attempting to output to a TextArea

6 ビュー (過去 30 日間)
Adam Graf
Adam Graf 2023 年 3 月 6 日
回答済み: Eric Delgado 2023 年 3 月 7 日
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 件のコメント
Adam Graf
Adam Graf 2023 年 3 月 7 日
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
Walter Roberson
Walter Roberson 2023 年 3 月 7 日
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.

サインインしてコメントする。

回答 (2 件)

Walter Roberson
Walter Roberson 2023 年 3 月 7 日
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.

Eric Delgado
Eric Delgado 2023 年 3 月 7 日
@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;

カテゴリ

Help Center および File ExchangeApp Building についてさらに検索

製品


リリース

R2022b

Community Treasure Hunt

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

Start Hunting!

Translated by