이 질문을 팔로우합니다.
- 팔로우하는 게시물 피드에서 업데이트를 확인할 수 있습니다.
- 정보 수신 기본 설정에 따라 이메일을 받을 수 있습니다.
pop up in address_book GUI demo with .mat file
조회 수: 2 (최근 30일)
이전 댓글 표시
Luhur
2012년 5월 27일
hi all...
Could anyone show me how to use pop up menu in address_book gui demo rather than use push button menu??
thanks
답변 (3개)
per isakson
2012년 5월 27일
댓글 수: 15
Luhur
2012년 5월 27일
would you mind explain it with the gui demo address_book.fig inside matlab??? cause my application similar with the demo and im not familiar with GUI..
thank you..
Walter Roberson
2012년 5월 27일
I do not have access to MATLAB today, so I cannot examine what they have done in that particular demo.
When you wrote "its different when using .mat file", did you mean that it is different when using GUIDE? The main difference when using GUIDE is that GUIDE likes to name the functions according to what kind of uicontrol is being used. The change at the MATLAB level between a pushbutton and a pop-up is very small, just set the uicontrol 'style' property to 'popup'
Luhur
2012년 5월 27일
okay, but please get back to me when you have access to matlab, i really need it, since it similar to my application. thanks
Luhur
2012년 5월 27일
the push button replace with pop up menu. from the demo address_book.fig it used 2 push button previous and next. the script:
% ------------------------------------------------------------
% Callback for the Prev and Next buttons
% Pass a string argument (str) indicating which object was selected
% ------------------------------------------------------------
function Prev_Next_Callback(hObject, eventdata, handles, str)
% hObject handle to Prev_Next (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
% Get the index pointer and the addresses
index = handles.Index;
Addresses = handles.Addresses;
% Depending on whether Prev or Next was clicked change the display
switch str
case 'Prev'
% Decrease the index by one
i = index - 1;
% If the index is less then one then set it equal to the index of the
% last element in the Addresses array
if i < 1
i = length(Addresses);
end
case 'Next'
% Increase the index by one
i = index + 1;
% If the index is greater than the size of the array then point
% to the first item in the Addresses array
if i > length(Addresses)
i = 1;
end
end
% Get the appropriate data for the index in selected
Current_Name = Addresses(i).Name;
Current_Phone = Addresses(i).Phone;
set(handles.Contact_Name,'string',Current_Name)
set(handles.Contact_Phone,'string',Current_Phone)
% Update the index pointer to reflect the new index
handles.Index = i;
guidata(hObject,handles)
Walter Roberson
2012년 5월 27일
To adapt, use this routine as the callback for the popup menu, but do not pass any string in to it.
Instead, at the top,
menuval = get(hObject, 'Value');
menustrings = cellstr( get(hObject, 'String') );
str = menustrings{menuval};
And that's it for the code changes, provided that you set the popup menu to have entries 'Prev' and 'Next'
Luhur
2012년 5월 28일
you mean delete the whole script mine above and change it with yours?? can you be more specific as i a beginner for this.. if u dont have access to the matlab demo then i wrote the hole code in the next answer...
Walter Roberson
2012년 5월 28일
function Prev_Next_Callback(hObject, eventdata, handles) %CHANGE
% hObject handle to Prev_Next (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
menuval = get(hObject, 'Value'); %NEW
menustrings = cellstr( get(hObject, 'String') ); %NEW
str = menustrings{menuval}; %NEW
% Get the index pointer and the addresses
index = handles.Index;
Addresses = handles.Addresses;
% Depending on whether Prev or Next was clicked change the display
switch str
case 'Prev'
% Decrease the index by one
i = index - 1;
[etc to the rest of the routine]
And have this routine be the callback for your popup menu.
Luhur
2012년 5월 28일
error walter... its sound:
??? Error using ==> address_book>Prev_Next_Callback
Too many input arguments.
Error in ==> gui_mainfcn at 96
feval(varargin{:});
Error in ==> address_book at 43
gui_mainfcn(gui_State, varargin{:});
Error in ==>
@(hObject,eventdata)address_book('Prev_Next_Callback',hObject,eventdata,guidata(hObject),'Next')
??? Error while evaluating uicontrol Callback
i already change the push button to pop up menu, with:
tag: Prev_Next
string: 1. Next
2. Prev
still error
any suggestion???
Walter Roberson
2012년 5월 28일
Use the property editor on that popup menu, and remove the comma and the 'Next' from the calling sequence.
Luhur
2012년 5월 28일
walter.. i have some similar question,,
what if i have 2 pop up menu that related. each other, at first i have 1 structure. that contain 2 member
let say:
Car.truck.a=10
Car.truck.b=20
and
Car.jeep.c=7
Car.jeep.d=14
the 2 pop up menu are related, for example if i chose popup1 the string i jeep and truck, then after chose 1 ,let say i chose jeep, then the 2nd popup menu (popup2) revealed option c or d, then after i chose d, the value of text is 14.
how can i built this kind of case??
thank you
Luhur
2012년 5월 29일
Any thought Mr Walter?? i also post it in a new question for the detail..
http://www.mathworks.com/matlabcentral/answers/39629-pop-up-menu-that-connected-and-read-from-mat-file
Luhur
2012년 5월 28일
function varargout = address_book(varargin)
% ADDRESS_BOOK Application M-file for address_book.fig
% ADDRESS_BOOK, by itself, creates a new ADDRESS_BOOK or raises the existing
% singleton*.
%
% H = ADDRESS_BOOK returns the handle to a new ADDRESS_BOOK or the handle to
% the existing singleton*.
%
% ADDRESS_BOOK('CALLBACK',hObject,eventData,handles,...) calls the local
% function named CALLBACK in ADDRESS_BOOK.M with the given input arguments.
%
% ADDRESS_BOOK('Property','Value',...) creates a new ADDRESS_BOOK or raises the
% existing singleton*. Starting from the left, property value pairs are
% applied to the GUI before address_book_OpeningFunction gets called. An
% unrecognized property name or invalid value makes property application
% stop. All inputs are passed to address_book_OpeningFcn via varargin.
%
% *See GUI Options - GUI allows only one instance to run (singleton).
%
% See also: GUIDE, GUIDATA, GUIHANDLES
% Edit the above text to modify the response to help address_book
% Copyright 2000-2006 The MathWorks, Inc.
% Last Modified by GUIDE v2.5 08-Apr-2002 16:42:00
% Begin initialization code - DO NOT EDIT
gui_Singleton = 1;
gui_State = struct('gui_Name', mfilename, ...
'gui_Singleton', gui_Singleton, ...
'gui_OpeningFcn', @address_book_OpeningFcn, ...
'gui_OutputFcn', @address_book_OutputFcn, ...
'gui_LayoutFcn', [], ...
'gui_Callback', []);
if nargin && ischar(varargin{1})
gui_State.gui_Callback = str2func(varargin{1});
end
if nargout
[varargout{1:nargout}] = gui_mainfcn(gui_State, varargin{:});
else
gui_mainfcn(gui_State, varargin{:});
end
% End initialization code - DO NOT EDIT
% --- Executes just before address_book is made visible.
function address_book_OpeningFcn(hObject, eventdata, handles, varargin)
% This function has no output args, see OutputFcn.
% hObject handle to figure
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
% varargin command line arguments to address_book (see VARARGIN)
% Choose default command line output for address_book
handles.output = hObject;
% Make figure non-dockable - added 1/5/10
% set(hObject,'DockControls','off') % added 1/5/10
set(hObject,'WindowStyle','normal') % added 1/5/10
set(hObject,'HandleVisibility','callback') % added 1/5/10
% Update handles structure
guidata(hObject, handles);
if nargin < 4
% Load the default address book
Check_And_Load([],handles);
% If first element in varargin is 'book' and the second element is a
% MATLAB file, then load that file
elseif (length(varargin) == 2 && strcmpi(varargin{1},'book') && (2 == exist(varargin{2},'file')))
Check_And_Load(varargin{2},handles);
else
errordlg('File Not Found','File Load Error')
set(handles.Contact_Name,'String','')
set(handles.Contact_Phone,'String','')
end
% UIWAIT makes address_book wait for user response (see UIRESUME)
% uiwait(handles.figure1);
% --- Outputs from this function are returned to the command line.
function varargout = address_book_OutputFcn(hObject, eventdata, handles)
% varargout cell array for returning output args (see VARARGOUT);
% hObject handle to figure
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
% Get default command line output from handles structure
varargout{1} = handles.output;
% ---
function pass = Check_And_Load(file,handles)
% Initialize the variable "pass" to determine if this is a valid file.
pass = 0;
% If called without any file then set file to the default file name.
% Otherwise if the file exists then load it.
if isempty(file)
file = 'addrbook.mat';
handles.LastFile = file;
guidata(handles.Address_Book,handles)
end
if exist(file,'file') == 2
data = load(file);
end
% Validate the MAT-file
% The file is valid if the variable is called "Addresses" and it has
% fields called "Name" and "Phone"
flds = fieldnames(data);
if (length(flds) == 1) && (strcmp(flds{1},'Addresses'))
fields = fieldnames(data.Addresses);
if (length(fields) == 2) && (strcmp(fields{1},'Name')) && (strcmp(fields{2},'Phone'))
pass = 1;
end
end
% If the file is valid, display it
if pass
% Add Addresses to the handles structure
handles.Addresses = data.Addresses;
% Display the first entry
set(handles.Contact_Name,'String',data.Addresses(1).Name)
set(handles.Contact_Phone,'String',data.Addresses(1).Phone)
% Set the index pointer to 1
handles.Index = 1;
% Save the modified handles structure
guidata(handles.Address_Book,handles)
else
errordlg('Not a valid Address Book','Address Book Error')
end
% ------------------------------------------------------------
% Callback for Open menu - displays an open dialog
% ------------------------------------------------------------
function Open_Callback(hObject, eventdata, handles, varargin)
% Use UIGETFILE to allow for the selection of a custom address book.
[filename, pathname] = uigetfile( ...
{'*.mat', 'All MAT-Files (*.mat)'; ...
'*.*','All Files (*.*)'}, ...
'Select Address Book');
% If "Cancel" is selected then return
if isequal([filename,pathname],[0,0])
return
% Otherwise construct the fullfilename and Check and load the file.
else
File = fullfile(pathname,filename);
% if the MAT-file is not valid, do not save the name
if Check_And_Load(File,handles)
handles.LastFIle = File;
guidata(hObject,handles)
end
end
% ------------------------------------------------------------
% Callback for the Contact Name text box
% ------------------------------------------------------------
function Contact_Name_Callback(hObject, eventdata, handles, varargin)
% hObject handle to Contact_Name (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
% Hints: get(hObject,'String') returns contents of edit1 as text
% str2double(get(hObject,'String')) returns contents of edit1 as a double
% Get the strings in the Contact Name and Phone text box
Current_Name = get(handles.Contact_Name,'string');
Current_Phone = get(handles.Contact_Phone,'string');
% If empty then return
if isempty(Current_Name)
return
end
% Get the current list of addresses from the handles structure
Addresses = handles.Addresses;
% Go through the list of contacts
% Determine if the current name matches an existing name
for i = 1:length(Addresses)
if strcmp(Addresses(i).Name,Current_Name)
set(handles.Contact_Name,'string',Addresses(i).Name)
set(handles.Contact_Phone,'string',Addresses(i).Phone)
handles.Index = i;
guidata(hObject,handles)
return
end
end
% If it's a new name, ask to create a new entry
Answer=questdlg('Do you want to create a new entry?', ...
'Create New Entry', ...
'Yes','Cancel','Yes');
switch Answer
case 'Yes'
Addresses(end+1).Name = Current_Name; % Grow array by 1
Addresses(end).Phone = Current_Phone;
index = length(Addresses);
handles.Addresses = Addresses;
handles.Index = index;
guidata(hObject,handles)
return
case 'Cancel'
% Revert back to the original number
set(handles.Contact_Name,'string',Addresses(handles.Index).Name)
set(handles.Contact_Phone,'String',Addresses(handles.Index).Phone)
return
end
% ------------------------------------------------------------
% Callback for the Contact Phone # text box
% ------------------------------------------------------------
function Contact_Phone_Callback(hObject, eventdata, handles, varargin)
% hObject handle to Contact_Phone (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
% Hints: get(hObject,'String') returns contents of edit1 as text
% str2double(get(hObject,'String')) returns contents of edit1 as a double
Current_Phone = get(handles.Contact_Phone,'string');
% If either one is empty then return
if isempty(Current_Phone)
return
end
% Get the current list of addresses from the handles structure
Addresses = handles.Addresses;
Answer=questdlg('Do you want to change the phone number?', ...
'Change Phone Number', ...
'Yes','Cancel','Yes');
switch Answer
case 'Yes'
% If no name match was found create a new contact
Addresses(handles.Index).Phone = Current_Phone;
handles.Addresses = Addresses;
guidata(hObject,handles)
return
case 'Cancel'
% Revert back to the original number
set(handles.Contact_Phone,'String',Addresses(handles.Index).Phone)
return
end
% ------------------------------------------------------------
% Callback for the Prev and Next buttons
% Pass a string argument (str) indicating which object was selected
% ------------------------------------------------------------
function Prev_Next_Callback(hObject, eventdata, handles, str)
% hObject handle to Prev_Next (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
% Get the index pointer and the addresses
index = handles.Index;
Addresses = handles.Addresses;
% Depending on whether Prev or Next was clicked change the display
switch str
case 'Prev'
% Decrease the index by one
i = index - 1;
% If the index is less then one then set it equal to the index of the
% last element in the Addresses array
if i < 1
i = length(Addresses);
end
case 'Next'
% Increase the index by one
i = index + 1;
% If the index is greater than the size of the array then point
% to the first item in the Addresses array
if i > length(Addresses)
i = 1;
end
end
% Get the appropriate data for the index in selected
Current_Name = Addresses(i).Name;
Current_Phone = Addresses(i).Phone;
set(handles.Contact_Name,'string',Current_Name)
set(handles.Contact_Phone,'string',Current_Phone)
% Update the index pointer to reflect the new index
handles.Index = i;
guidata(hObject,handles)
% ------------------------------------------------------------
% Callback for Save and Save As menus
% ------------------------------------------------------------
function Save_Callback(hObject, eventdata, handles, varargin)
% Get the Tag of the menu selected
Tag = get(hObject,'Tag');
% Get the address array
Addresses = handles.Addresses;
% Based on the item selected, take the appropriate action
switch Tag
case 'Save'
% Save to the default addrbook file
File = handles.LastFile;
save(File,'Addresses')
case 'Save_As'
% Allow the user to select the file name to save to
[filename, pathname] = uiputfile( ...
{'*.mat';'*.*'}, ...
'Save as');
% If 'Cancel' was selected then return
if isequal([filename,pathname],[0,0])
return
else
% Construct the full path and save
File = fullfile(pathname,filename);
save(File,'Addresses')
handles.LastFile = File;
guidata(hObject,handles)
end
end
% ------------------------------------------------------------
% Callback for the Contact --> Create New menu
% ------------------------------------------------------------
function New_Callback(hObject, eventdata, handles, varargin)
set(handles.Contact_Name,'String','')
set(handles.Contact_Phone,'String','')
% function ResizeFcn(hObject, eventdata, handles, varargin)
% return
% ------------------------------------------------------------
% Callback for the GUI figure ResizeFcn property.
% ------------------------------------------------------------
function ResizeFcn(hObject, eventdata, handles, varargin)
% Handles resize behavior except when docked. This is because a certain
% window height is always preserved, and because docked windows can
% have arbitrary proportions.
% Get the figure size and position. Figure Units are fixed as 'pixels'.
% uicontrol units are in 'characters'
Figure_Size = get(hObject,'Position');
% This is the figure's original position in pixel units
Original_Size = [350 700 470 250];
% If the figure seems to be maximized, do not resize at all
pix_pos = get(hObject,'Position');
scr_size = get(0,'ScreenSize');
if .99*scr_size(3) < pix_pos(3) % Apparently maximized
% When docked, get out
return
end
% If the resized figure is smaller than the original figure size then compensate
% However, do not change figure size if it is docked; just adjust
% uicontrols
if ~strcmp(get(hObject,'WindowStyle'),'docked')
if Figure_Size(3) < Original_Size(3)
% If the width is too small then reset to origianl width
set(hObject,'Position',[Figure_Size(1) ...
Figure_Size(2) ...
Original_Size(3) ...
Original_Size(4)])
Figure_Size = get(hObject,'Position');
end
if abs(Figure_Size(4) - Original_Size(4)) > 10 % pixels
% Do not allow the height to change
set(hObject,'Position',[Figure_Size(1) ...
Figure_Size(2)+Figure_Size(4)-Original_Size(4) ...
Figure_Size(3) ...
Original_Size(4)])
end
movegui(hObject, 'onscreen')
end
% Get Contact_Name field Position for readjusting its width
C_N_pos = get(handles.Contact_Name,'Position');
ratio = Figure_Size(3) / Original_Size(3);
% Reset it so that its width remains proportional to figure width
% The original width of the Contact_Name box is 72 (characters)
set(handles.Contact_Name,'Position',[C_N_pos(1) ...
C_N_pos(2) ...
ratio * 72 ...
C_N_pos(4)])
% --- Executes during object creation, after setting all properties.
function Contact_Name_CreateFcn(hObject, eventdata, handles)
% hObject handle to Contact_Name (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles empty - handles not created until after all CreateFcns called
% Hint: edit controls usually have a white background, change
% 'usewhitebg' to 0 to use default. See ISPC and COMPUTER.
usewhitebg = ispc;
if usewhitebg
set(hObject,'BackgroundColor','white');
else
set(hObject,'BackgroundColor',get(0,'defaultUicontrolBackgroundColor'));
end
% --- Executes during object creation, after setting all properties.
function Contact_Phone_CreateFcn(hObject, eventdata, handles)
% hObject handle to Contact_Phone (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles empty - handles not created until after all CreateFcns called
% Hint: edit controls usually have a white background, change
% 'usewhitebg' to 0 to use default. See ISPC and COMPUTER.
usewhitebg = ispc;
if usewhitebg
set(hObject,'BackgroundColor','white');
else
set(hObject,'BackgroundColor',get(0,'defaultUicontrolBackgroundColor'));
end
Luhur
2012년 5월 29일
Any thought Mr Walter?? i also post it in a new question for the detail.. http://www.mathworks.com/matlabcentral/answers/39629-pop-up-menu-that-connected-and-read-from-mat-file
참고 항목
카테고리
Help Center 및 File Exchange에서 Entering Commands에 대해 자세히 알아보기
태그
제품
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!오류 발생
페이지가 변경되었기 때문에 동작을 완료할 수 없습니다. 업데이트된 상태를 보려면 페이지를 다시 불러오십시오.
웹사이트 선택
번역된 콘텐츠를 보고 지역별 이벤트와 혜택을 살펴보려면 웹사이트를 선택하십시오. 현재 계신 지역에 따라 다음 웹사이트를 권장합니다:
또한 다음 목록에서 웹사이트를 선택하실 수도 있습니다.
사이트 성능 최적화 방법
최고의 사이트 성능을 위해 중국 사이트(중국어 또는 영어)를 선택하십시오. 현재 계신 지역에서는 다른 국가의 MathWorks 사이트 방문이 최적화되지 않았습니다.
미주
- América Latina (Español)
- Canada (English)
- United States (English)
유럽
- Belgium (English)
- Denmark (English)
- Deutschland (Deutsch)
- España (Español)
- Finland (English)
- France (Français)
- Ireland (English)
- Italia (Italiano)
- Luxembourg (English)
- Netherlands (English)
- Norway (English)
- Österreich (Deutsch)
- Portugal (English)
- Sweden (English)
- Switzerland
- United Kingdom (English)
아시아 태평양
- Australia (English)
- India (English)
- New Zealand (English)
- 中国
- 日本Japanese (日本語)
- 한국Korean (한국어)