AppDesigner - Changing EditField font size

3 views (last 30 days)
Hi! I wanted to change the font size of an edit field while running the program by selecting the value from the drop down list. This is the event function:
function FontSizeDropDownValueChanged(app, event)
value = app.FontSizeDropDown.Value;
switch value
case 8
app.EditField.FontSize = 8;
case 10
app.EditField.FontSize = 10;
case 12
app.EditField.FontSize = 12;
case 14
app.EditField.FontSize = 14;
case 16
app.EditField.FontSize = 16;
end
app.EditField.Value = value;
end
and this is the idea:
However, when I select a value the font size doesn't change. The text does change, but not the font size. What mistake am I making?

Accepted Answer

Adam Danz
Adam Danz on 24 May 2021
Edited: Adam Danz on 26 May 2021
My guess is that your list of font sizes are strings and the value returned to the callback function is a string. Since it's a string it's not matching any of the cases in the switch/case so the fontsize never changes.
Option 1 is to convert the string to a number using str2double.
Option 2 is to define the dropdown ItemsData property as numbers, not strings. Then the Value will return a string. See properties for more info.
Lastly, stop using a switch case. Assign the fontsize directly
function FontSizeDropDownValueChanged(app, event)
value = app.FontSizeDropDown.Value;
app.EditField.FontSize = value; % or str2double(value) depending on which option you choose
app.EditField.Value = value; % or num2str(value) if a string is needed.
end

More Answers (0)

Categories

Find more on Environment and Settings in Help Center and File Exchange

Products


Release

R2021a

Community Treasure Hunt

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

Start Hunting!