Hey guys,
I have completed my program last week and I have been asked by my supervisor to create a GUI for it. I have no knowledge about GUI and I have never worked on anything similar before.
I have two objects in my UI. I have a pop-up menu and a push button. The pop-up button gives 4 options and the push button is a START button where my code will start running as I it would normally do without the GUI.
Buy choosing something in the drop down menu and then pressing start I would like the program to set a variable equal to something and then start running my program. (hyper.m)
Any Ideas?
Thanks guys.

답변 (2개)

Evan
Evan 2012년 12월 14일
편집: Evan 2012년 12월 14일

0 개 추천

The way I would do things is put it all in the callback of your pushbutton. The "value" property of your popupmenu will allow to determine which option the user has selected, so you can get that from within the pushbutton callback.
So something like this, within the callback of your pushbutton:
v = get(handles.mypopupmenu,'Value'); %get currently selected option from menu
if v == 1
%stuff here
elseif v == 2
%stuff here
elseif v == 3
%stuff here
elseif v == 4
%stuff here
end
It's sort of hard to get specific beyond that, but hopefully that gives you a good starting idea. The main point is that the "get" function lets you get the properties of any uicontrol saved in handles.
Matt Fig
Matt Fig 2012년 12월 14일
편집: Matt Fig 2012년 12월 14일

0 개 추천

There are many ways to do this. Here is a simple example that may provide a skeleton for you to fill out.
function [] = gui_pop()
% Help goes here.
S.fh = figure('units','pixels',...
'position',[300 300 300 100],...
'menubar','none',...
'name','gui_pop',...
'numbertitle','off',...
'resize','off');
S.pb = uicontrol('style','push',...
'unit','pix',...
'position',[10 15 280 20],...
'fontsize',12,...
'fontweight','bold',...
'string','START',...
'callback',@pb_call);
S.pp = uicontrol('style','pop',...
'unit','pix',...
'position',[10 60 280 20],...
'fontsize',12,...
'fontweight','bold',...
'string',{'1';'2';'3';'4'},...
'value',1);
guidata(S.fh,S)
movegui('center')
function [] = pb_call(varargin)
% Callback for pushbutton.
S = guidata(gcbf);
N = get(S.pp,{'string','value'});
N = str2double(N{1}(N{2})); % Convert to number. May not need.
% Pass N to a function and display the results.
% This is where you can call any function on the path.
% I converted to a number, but you may need to keep the string
% depending on what your function expects. I have no clue what
% your function does or what will be in the popup, So I give a
% general example.
home
2^N

댓글 수: 4

Rahul jain
Rahul jain 2015년 8월 23일
편집: Walter Roberson 2015년 8월 23일
u had written N = get(S.pp,{'string','value'});
it give me size of string and value in pop up menu but if i have string character in t and i want to get what string is it not the number so what i have to do
pop_menu = uicontrol('Parent', gui, 'Style', 'Popupmenu', 'Units', 'normalized', 'Position', [.18 .08 .3 .15], 'String',{'red','green','orange'}, 'Value', 3, 'Callback', @popup_bar);
function [] = popup_bar(object,handles)
pop_value=get(pop_menu,{'String','Value'})
end
in this code i want the value like red green orange not the position where is my green or red.
please help as soon as possible
pop_strings = get(pop_menu, 'String');
pop_val = get(pop_menu, 'Value');
result = pop_strings{pop_val};
Ronn Concepcion II
Ronn Concepcion II 2017년 5월 25일
May I know the codes how to get the selected string(characters) on the pop-up menu? I will be using that string as a value of a variable. In my case, the pop-up menu contains the .csv filenames in a folder located at desktop. then after selecting the filename, it automatically plot the data on axis.
Doesn't Walter's code do that? If you want a more modern OOP way, do it like this:
popStrings = handles.pop_menu.String; % All the strings in the menu.
selectedIndex = handles.pop_menu.Value;
selectedString = popStrings{selectedIndex};

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

카테고리

도움말 센터File Exchange에서 Simulink Environment Customization에 대해 자세히 알아보기

질문:

2012년 12월 14일

댓글:

2017년 5월 25일

Community Treasure Hunt

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

Start Hunting!

Translated by