필터 지우기
필터 지우기

Question Regarding Callbacks and UI Control

조회 수: 6 (최근 30일)
Jon
Jon 2022년 12월 20일
편집: Voss 2022년 12월 20일
Hello,
I would like to make a UI Control option that allows the user to select one of N options, where N depends on the imported data.
Each option will correspond to a different table within a structure, and my plan is to plot the data based on the selection of the user.
My original intentions were to use a Radio Button option, but after reading this help sheet, I think I can accomplish my goal with a PopUp Menu as well.
I copied the code below:
function mytemps
f = figure;
c = uicontrol(f,'Style','popupmenu');
c.Position = [20 75 60 20];
c.String = {'Celsius','Kelvin','Fahrenheit'};
c.Callback = @selection;
function selection(src,event)
val = c.Value;
str = c.String;
str{val};
disp(['Selection: ' str{val}]);
end
end
I'm just learning about UI Control, so I had a couple questions to help me understand.
1. How could I make the string a dependent variable?
My intuition is to set up a for loop to just change each cell in the structure to a string, something like:
for i=1:N
structure(i).day = num2str(structure(i).day)
end
Where each element in the structure.day is a number starting at 1 and going to N by one (something like 1,2,3,...,N).
Then I can save this as a new array itself, and input this into the c.String field.
This seems a little expensive though, but wanted to check if it was logical.
2. Based on other forum posts I've read, to use the user's input from a PopUp Menu (or any UI Control style), you need to set up a Callback function.
In the copied code above, the author only displays the user's input.
If I want to use it to reference the structure array such that the script knows what to plot, I would have to do something like this, correct?
function selection(src,event)
val = c.Value;
str = c.String;
target = str2num(val);
plot(time_vector,structure(target).Data)
end
I'm not sure if this makes sense, I want the target to be a number (along 1:1:N).
3. Could someone explain further what the src and event inputs represents in the line below?
function selection(src,event)
Are those byproducts of using uicontrol?
Thanks for advance for helping to clarify some of this.
  댓글 수: 1
Rik
Rik 2022년 12월 20일
For general advice and examples for how to create a GUI (and avoid using GUIDE), have look at this thread. I suspect that will help you answer most of your questions.

댓글을 달려면 로그인하십시오.

답변 (1개)

Voss
Voss 2022년 12월 20일
편집: Voss 2022년 12월 20일
"1. How could I make the string a dependent variable?"
If you want the popupmenu to show the value of the "day" field for every element of the struct array "structure", then you can do something like this (assuming each "day" is an integer):
c.String = sprintfc('%d',[structure.day]);
Or if you want the popupmenu to show '1', '2', '3', ..., up to N, where N is the number of elements of the structure array:
c.String = sprintfc('%d',1:numel(structure));
Or if you want it to show 'Day 1', 'Day 2', etc.:
c.String = sprintfc('Day %d',1:numel(structure));
"2. ... set up a Callback function."
That's right, you need a Callback function. The Value of a popupmenu is the index in its String of the currently selected item. Since Value is a numeric index, you don't need to use str2num on Value and you can use it directly as an index in your structure array:
function selection(src,event)
val = c.Value;
plot(time_vector,structure(val).Data)
end
That code assumes that time_vector is defined in the main function and the callback function ("selection") is nested under that main function (as it is in the copied code you posted). Also, it assumes that time_vector doesn't depend on the value selected in the popupmenu (i.e., time_vector is the same for all data sets).
"3. Could someone explain further what the src and event inputs represents in the line below?"
src is the handle of the uicontrol whose callback is selection (or whatever callback function you're writing). Your popupmenu is called "c" in the parent function, and you use "c" in the nested callback; that is fine, but you could use src inside the callback to refer to the popupmenu instead:
function selection(src,event)
val = src.Value;
plot(time_vector,structure(val).Data)
end
event is not very useful for uicontrols (I believe it is always empty for uicontrols), but it is useful for uitables and other things.
"Are those byproducts of using uicontrol?"
That's essentially correct. Any function handle used as a uicontrol's callback, e.g. "s.Callback = @selection", will get those two input arguments automatically by virtue of being a uicontrol callback.
As an aside, if you need additional input arguments in your callback function, you can specify the callback as a cell array containing the function handle followed by the additional arguments. For instance, you could pass time_vector in as an additional argument like this:
s.Callback = {@selection,time_vector};
function selection(src,event,time_vector)
val = src.Value;
plot(time_vector,structure(val).Data)
end
Be aware that the value of time_vector (or any additional argument you use) seen in selection will be its value when the s.Callback = {@selection,time_vector}; line is executed. If time_vector changes later, selection won't have the updated version. You'd have to do s.Callback = {@selection,time_vector}; again with the new time_vector for selection to get the new value the next time it executes.

카테고리

Help CenterFile Exchange에서 Migrate GUIDE Apps에 대해 자세히 알아보기

제품


릴리스

R2021b

Community Treasure Hunt

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

Start Hunting!

Translated by