Print Multiple Lines into TextArea Automatically without mentioning the Array (MATLAB App Designer)

35 views (last 30 days)
Hello,
I am trying to create like a message box into app designer using the Text Area component. For example if the user enter the Name and LastName it displays 'Name and LastName Entered successfully'. Else the Text Area displays 'Information Not Provided'. So now when the user selects Gender from drop down box it should displays 'Gender as Male selected'. Text is displayed into the Text Area when a Button is clicked. The button has a functionality to check whether the textField is empty or not empty. And to check whether the textField is empty its done with a help of function checkNames(app). To check the selected Gender from the dropDown Menu I am using a Switch case and the switch case is evaluated using the function checkGender(app). Is there a way so that i dont have to define .Value{1}, .Value{2} .... Thank You
Code
methods (Access = private)
function checkNames(app)
if ~(isempty(app.EditField_Name.Value) || isempty(app.EditField_LastName.Value))
app.MessageBoxTextArea.Value{1} = 'Name and LastName Entered successfully';
else
app.MessageBoxTextArea.Value{1} = 'Information Not Provided';
uiwait(app.checkNames);
end
end
function checkGender(app)
switch app.DropDown_Gender.Value
case 'Male'
app.MessageBoxTextArea.Value{2} = 'Gender as Male Selected';
case 'Female'
app.MessageBoxTextArea.Value{2} = 'Gender as Female Selected';
otherwise
app.MessageBoxTextArea.Value{2} = 'Gender as Others Selected';
end
end
end
% Callbacks that handle component events
methods (Access = private)
% Button pushed function: Button
function ButtonPushed(app, event)
checkNames(app);
checkGender(app);
end
end
  4 Comments
Rahul
Rahul on 3 Jan 2023
Thank you @Geoff Hayes. It worked for me! Can we also add the functionality of changing the text colour? for example if its 'Data Entered Sucessfully' it should display in the text area with Green as the font colour. And when it is an error it is displayed with a font colour of Red?
Geoff Hayes
Geoff Hayes on 6 Jan 2023
@Rahul - I don't think you can do that for individual lines. There may be some undocumented means of doing that sort of thing (wrapping text in html tags?) but I'm not sure how to do that.

Sign in to comment.

Accepted Answer

Geoff Hayes
Geoff Hayes on 21 Dec 2022
Edited: Geoff Hayes on 21 Dec 2022
@Rahul - you could add a helper function/method that will add the text for you at the correct index. For example, that function may look like
function addMessageToMessageBox(app, msg)
x = length(app.MessageBoxTextArea.Value);
app.MessageBoxTextArea.Value{x+1} = msg;
end
and then your code would become
function checkNames(app)
if ~(isempty(app.EditField_Name.Value) || isempty(app.EditField_LastName.Value))
addMessageToMessageBox(app, Name and LastName Entered successfully');
else
addMessageToMessageBox(app, 'Information Not Provided');
uiwait(app.checkNames);
end
end
function checkGender(app)
switch app.DropDown_Gender.Value
case 'Male'
addMessageToMessageBox(app, 'Gender as Male Selected');
case 'Female'
addMessageToMessageBox(app, 'Gender as Female Selected');
otherwise
addMessageToMessageBox(app, 'Gender as Others Selected');
end
end

More Answers (0)

Categories

Find more on Develop uifigure-Based Apps 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!