Error in popup menu GUI reading from .mat file

1 Ansicht (letzte 30 Tage)
Luhur
Luhur am 29 Mai 2012
hi all,
i have a simple application that shows name and contact number with pop up menu. the popup menu string set to be 4 item, each item shows name and contact:
here is my code:
function varargout = address_book(varargin)
% 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
% 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)
else
errordlg('Not a valid Address Book','Address Book Error')
end
% ------------------------------------------------------------
% Callback for the Pilih buttons
% Pass a string argument (str) indicating which object was selected
% ------------------------------------------------------------
function Pilih_Callback(hObject, eventdata, handles) %CHANGE
% hObject handle to Pilih (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
switch str
case 'Satu'
set(handles.Contact_Name,'String',Addresses(handles.addresses(1)).Name)
set(handles.Contact_Phone,'String',Addresses(handles.addresses(1)).Phone)
case 'Dua'
set(handles.Contact_Name,'String',Addresses(handles.addresses(2)).Name)
set(handles.Contact_Phone,'String',Addresses(handles.addresses(2)).Phone)
case 'Tiga'
set(handles.Contact_Name,'String',Addresses(handles.addresses(3)).Name)
set(handles.Contact_Phone,'String',Addresses(handles.addresses(3)).Phone)
case 'Empat'
set(handles.Contact_Name,'String',Addresses(handles.addresses(4)).Name)
set(handles.Contact_Phone,'String',Addresses(handles.addresses(4)).Phone)
end
% Get the appropriate data for the index in selected
set(handles.Contact_Name,'string',Current_Name)
set(handles.Contact_Phone,'string',Current_Phone)
% Update the index pointer to reflect the new index
guidata(hObject,handles)
and here is the error:
??? Reference to non-existent field 'addresses'.
Error in ==> address_book>Pilih_Callback at 155
set(handles.Contact_Name,'String',Addresses(handles.addresses(2)).Name)
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('Pilih_Callback',hObject,eventdata,guidata(hObject))
??? Error while evaluating uicontrol Callback
any suggestion??
thanks

Antworten (1)

Walter Roberson
Walter Roberson am 29 Mai 2012
You did not create a field handles.addresses . You did, though, create a field handles.Addresses (with a capital A)
  5 Kommentare
Luhur
Luhur am 30 Mai 2012
still error...
Walter Roberson
Walter Roberson am 30 Mai 2012
You used load() to bring in addrbook.mat, resulting in the structure named data. There is a field named "Addresses" (capital-A) inside that "data" structure. You copy that field to handles.addresses, and you arrange so that the callback has access to handles. Then in your callback, you take handles.addresses(1) which appears to be a structure array. You then try to use that structure array to index "addresses", which you indicate is a field inside addrbook.mat but there is no evidence that "addresses" with a lower-case "A" existed there. If it did exist there, it would have been stored into data.addresses. But supposing you had all the upper/lower case straightened out, you appear to be saying that you want to index the Addresses struct at a structure array. Indexing something at a structure array is not defined.
Hint:
set(handles.Contact_Name,'String',handles.addresses(1).Name)

Melden Sie sich an, um zu kommentieren.

Kategorien

Mehr zu Migrate GUIDE Apps finden Sie in Help Center und File Exchange

Produkte

Community Treasure Hunt

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

Start Hunting!

Translated by