uicontrol issues on figure

1 view (last 30 days)
Isaiah Slemons
Isaiah Slemons on 24 May 2019
Edited: Isaiah Slemons on 28 May 2019
I am trying to use uicontrol to create a menu. I can't figure how to query the value of the popupmenu and I don't understand local function defintion.
X = readcell('Data.xlsx');
Matrix = cellstr(X(:,1));
dd = uicontrol(fig,'Style','popupmenu','Position',[20 100 80 250],'String', Matrix,...?);
X is an excel spredsheet that contains data in the follow:
String Value Value Value
I've read in the string portion of the sheet and now I want to query the value when I click something in the popupmenu. Again, I am not familiar with local function definition and it confuses me. I know that a solution may invole using a callback/buttondownfcn but I dont understand how to query those in the same script. Thanks

Accepted Answer

Rik
Rik on 24 May 2019
It may take some getting used to how to build a GUI with Matlab, but once you get the hang of it you'll find it gets easy. To keep your brain from melting, don't use GUIDE. Either use the AppDesigner, or use normal tools. Since you seem to already be doing the latter:
My small guide to avoid GUIDE:
  • Make a figure (with f=figure;) and look into the doc for figure which properties you want to turn off (you probably want to set Menu and Toolbar to 'none')
  • Create buttons and axes and everything you need with functions like uicontrol and axes. Save the handles to each element to fields of a struct (like handles.mybutton=uicontrol(___);)
  • When you've finished loading all data (and saving it to fields of your handles struct), and creating all the buttons, save your handles struct to the guidata of your figure like this guidata(handles.f,handles);. (You can also use getappdata and setappdata)
  • You can set the Callback property of many objects. If you do, use a function name with an @ in front, or a char array that can be evaluated to valid code. (like @MyFunction or 'disp(''you pushed the button'')')
  • Callback functions will be called with two arguments: the first is a handle to the callback object, the second is eventdata that may contain special information. To get access to your data, just use handles=guidata(gcbo);. You can replace the gcbo function with the name of the first input to your callback function if you prefer.
  • More information about callbacks can be found in multiple places in the doc, for example here.
So when getting the values from your matrix you can do something like this:
h=struct;%will be stored to guidata
h.fig=figure;
X = readcell('Data.xlsx');
Matrix = cellstr(X(:,1));
h.dd = uicontrol(fig,'Style','popupmenu',...
'Position',[20 100 80 250],...
'Value',1,...
'String', Matrix,...
'Callback',@DoSomething);
h.X=X;
guidata(h.fig,h)
function DoSomething(hObject,eventdata)
h=guidata(hObject);
val=get(h.dd,'Value');
value_list_from_X=X(val,2:end);
%do something with that variable here
end
  1 Comment
Isaiah Slemons
Isaiah Slemons on 28 May 2019
Edited: Isaiah Slemons on 28 May 2019
Thank you very much. This explanation helped a lot in my understanding.

Sign in to comment.

More Answers (0)

Categories

Find more on Migrate GUIDE Apps in Help Center and File Exchange

Products

Community Treasure Hunt

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

Start Hunting!