MATLAB Answers


uicontrol issues on figure

Isaiah Slemons さんによって質問されました 2019 年 5 月 24 日
最新アクティビティ Isaiah Slemons さんによって 編集されました 2019 年 5 月 28 日
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

  0 件のコメント

サインイン to comment.

1 件の回答

回答者: Rik
2019 年 5 月 24 日

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
X = readcell('Data.xlsx');
Matrix = cellstr(X(:,1));
h.dd = uicontrol(fig,'Style','popupmenu',...
'Position',[20 100 80 250],...
'String', Matrix,...
function DoSomething(hObject,eventdata)
%do something with that variable here

  1 件のコメント

Isaiah Slemons 2019 年 5 月 28 日
Thank you very much. This explanation helped a lot in my understanding.

サインイン to comment.

Translated by