MATLAB: How to use a drop down to make an edit field or button invisible in the app designer

callbackdrop downdropdownedit fieldvisibilityvisible

In app designer, I have a drop down for the number of mobile base stations, which based on the number, the user have to input the coordinates for each of those base stations. For example, if you choose 2 from the drop down, you have to input a set of X and Y for each of those base stations.
Now I have created one edit field for every coordinate input… 4 in total, which all are visible. What I want is to have only the first one visible. If the user chose 2 from the drop down menu, the second edit field shows up, and if 3, the third one appears, and so on.
There is the HandleVisibility and Visible interactive control. I tried setting the edit fields handle on callback, so that I can toggle the visibility of other fields in the callback using an if statement.
BaseStation = app.BaseStation_DropDown.Value;
if PhoneNumber == 1
app.User2Label.Visible,'off';
app.User3Label.Visible,'off';
app.User4Label.Visible,'off';
end
Whatever I did, it's not working. Any help is appreciated.
Thanks
//

Best Answer

Handle visibility is not the setting you want. The Visible property must be set to either 'off' or 'on' to hide or show it, respectively. An edit field has two components, the edit field and the associated label:
app.EditField.Visible = 'off';
app.EditFieldLabel.Visible = 'off';
It sounds like you want to start with them set to off, and turn them on depending on what value is selected in a dropdown menu. I would uncheck Visible when placing the edit fields in Design View and do something like this for the drop down callback:
% Value changed function: DropDown
function DropDownValueChanged(app, event)
value = app.DropDown.Value;
switch value
case '1'
app.EditField2.Visible = 'off';
app.EditField2Label.Visible = 'off';
app.EditField3.Visible = 'off';
app.EditField3Label.Visible = 'off';
app.EditField4.Visible = 'off';
app.EditField4Label.Visible = 'off';
case '2'
app.EditField2.Visible = 'on';
app.EditField2Label.Visible = 'on';
app.EditField3.Visible = 'off';
app.EditField3Label.Visible = 'off';
app.EditField4.Visible = 'off';
app.EditField4Label.Visible = 'off';
case '3'
app.EditField2.Visible = 'on';
app.EditField2Label.Visible = 'on';
app.EditField3.Visible = 'on';
app.EditField3Label.Visible = 'on';
app.EditField4.Visible = 'off';
app.EditField4Label.Visible = 'off';
case '4'
app.EditField2.Visible = 'on';
app.EditField2Label.Visible = 'on';
app.EditField3.Visible = 'on';
app.EditField3Label.Visible = 'on';
app.EditField4.Visible = 'on';
app.EditField4Label.Visible = 'on';
end
end
I'm assuming you want all fields up to the number selected to display. If not, just remove the corresponding code from the case statements. Note that a drop down menu returns a char, even if you only have it list numbers. Because value is a char, my case statements are chars. I could have just as easily used switch str2num(value) with my case statements being numbers.
value = app.DropDown.Value;
switch str2num(value)
case 2
...
I suspect value being a char has something to do with why your current implementation is not working. With the code you posted above, what happens if you change your if statement to
PhoneNumber = app.BaseStation_DropDown.Value;
if str2num(PhoneNumber) == 1
app.User2Label.Visible,'off';
app.User3Label.Visible,'off';
app.User4Label.Visible,'off';
end