GUI to launch a function based on dropdown list

8 ビュー (過去 30 日間)
David malins
David malins 2012 年 4 月 27 日
Hi there, I'm trying to create a GUI that has a drop down list, the names correspond to different functions I would like to execute when I hit a 'load data' button. There is a simple gui in the examples but this preloads data from function; peaks, membrane etc. I want to select first and then execute. The functions go off an interogate a datbase.
hope someone has a sample gui i could use please?
dave

回答 (3 件)

Doug Hull
Doug Hull 2012 年 4 月 27 日
The key concepts you will need here:
Use
get(handles.popupmenu1, 'string')
get(handles.popupmenu1, 'value')
to get the selected string.
Use a switch case to choose among the options.
In each option, you will need to create a function handle to the appropriate function, ie
fh = @interogateMethodA
or
fh = @interogateMethodB
etc.
Then you can use feval, or just fh(inputs) to execute the arbitrary function with whatever inputs you wish.

Walter Roberson
Walter Roberson 2012 年 4 月 27 日
dispatch_table = {@fun1, @fun2, @fun3};
thisfun = dispatch_table(get(TheHandle, 'Value'));
thisfun(AppropriateArguments);
  1 件のコメント
Doug Hull
Doug Hull 2012 年 4 月 27 日
The only thing I don't like here is if someone else modifies the order of the choices, or adds to them, this breaks (potentially). My switch case is more bullet proof to the common situation of someone messing with the GUI and not realizing the downstream impact.

サインインしてコメントする。


David malins
David malins 2012 年 4 月 27 日
Doug, Thanks for this. Just to clarify, would I do the get 'string' and value then do the switch case and function handle all under the 'popup' call back. Then use feval fh under the 'puchbutton' callback?
cheesr
  1 件のコメント
Walter Roberson
Walter Roberson 2012 年 4 月 27 日
strs = cellstr(get(handles.popupmenu1, 'string'));
thisstr = strs{get(handles.popupmenu1, 'value')};
You would probably do this at the pushbutton callback.

サインインしてコメントする。

カテゴリ

Help Center および File ExchangeInteractive Control and Callbacks についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by