Displaying the result of a pushbutton command in AppDesigner

16 views (last 30 days)
Hello MATLAB Community,
I am trying to use the App Designer so that when I click the blue push button (this generates a random value), that randomly generated value will be displayed in the numeric edit field below.
Below I have what I tried: When the push button is clicked, a random value is generated.
The display(myfluor) displays the value in the command window but not the GUI. I attempted to assign the Value of the numeric edit field to the random variable but when the program is run, the field does not change. Any suggestions would be greatly appreciated, thank you!
% Button pushed function:
% RandomFluorescenceValueGeneratorButton
function RandomFluorescenceValueGeneratorButtonPushed(app, event)
a = 0; %min fluorescence
b = 1600; %max fluorescence
r = (b-a).*rand(1600,1) + a; %random vector of values generated from range a to b
myfluor = (randi(length(r))); %picking a random value in vector r to be the fluorescence value
display(myfluor)
end
% Value changed function: FluorescenceEditField
function FluorescenceEditFieldValueChanged(app, event)
app.FluorescenceEditField.Value = myfluor;
end

Accepted Answer

Geoff Hayes
Geoff Hayes on 8 Apr 2020
Edited: Geoff Hayes on 8 Apr 2020
Tiffany - in your pushbutton callback
function RandomFluorescenceValueGeneratorButtonPushed(app, event)
a = 0; %min fluorescence
b = 1600; %max fluorescence
r = (b-a).*rand(1600,1) + a; %random vector of values generated from range a to b
myfluor = (randi(length(r))); %picking a random value in vector r to be the fluorescence value
display(myfluor)
end
the display(myfluor) is responsible for what you see in the command window. If you want to populate the edit control, you would need to add a line to do that
% Button pushed function:
% RandomFluorescenceValueGeneratorButton
function RandomFluorescenceValueGeneratorButtonPushed(app, event)
a = 0; %min fluorescence
b = 1600; %max fluorescence
r = (b-a).*rand(1600,1) + a; %random vector of values generated from range a to b
myfluor = (randi(length(r))); %picking a random value in vector r to be the fluorescence value
display(myfluor);
app.FluorescenceEditField.Value = myfluor;
end
% Value changed function: FluorescenceEditField
function FluorescenceEditFieldValueChanged(app, event)
% nothing needed here
end
Note that I just moved the line from the FluorescenceEditFieldValueChanged to the button callback. The FluorescenceEditFieldValueChanged would be called when you make a change (via the GUI) to this field which is not what is happening here.
  1 Comment
TYT
TYT on 9 Apr 2020
Thank you very much! What belongs in which function makes sense now, thanks again.

Sign in to comment.

More Answers (0)

Categories

Find more on Biomedical Imaging 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!