Error while evaluating uicontrol Callback (GUI_handle problem)
44 views (last 30 days)
Show older comments
hi everybody, my problem is about programmed GUI. first I read "mean" and "standard deviation" for a vector from GUI and than create a vector depends on those values to show all values to user to make them editable also. Up to now I have succeed to create the the editable table but the values does not look like as user has initialized, I guess it is because of str2double and num2str conversions, because in the function that I want to make the calculation it gives error of
%%%%%%%%%% ??? Reference to non-existent field 'R_leak_1_mean_value'. Error in ==> GUI_Module>Button_Calculate at 150 h.R_leak_1_value =ones(48,1).*h.R_leak_1_mean_value + randn(48,1).*h.R_leak_1_std_value; ??? Error while evaluating uicontrol Callback %%%%%%%%% here infact when I run simulation step by step in the handle of "h" I can see R_leak_1_mean_value which is a conversed value of string of the same variable but whenever I go another function of where I want to create the array it gives this error. Although I use at the first and last line of each
function
" variables =guidata(h.fig);" and "guidata(h.fig,variables);" respectively I think still I cannot save and call the variable whenever I need I hope I have clearly defined the problem I have given the m-file that I've written for GUI any suggestion will appreciated thanks in advance:)
if true function GUI_module h.fig = figure('units','pixel','OuterPosition',[2 3 750 450],'ToolBar','none','MenuBar','none','Name','Balancing Model','NumberTitle','off'); variables =guidata(h.fig); %% Text boxes h.components_text = uicontrol('style','text','units','normalized','position',[1/20 36/40 0.46 1/20], 'HorizontalAlignment','center','string','Initialization'); h.mean_text = uicontrol('style','text','units','normalized','position',[6/20 33/40 0.1 1/20], 'HorizontalAlignment','center','string','Mean value'); h.std_text = uicontrol('style','text','units','normalized','position',[8.15/20 33/40 0.1 1/20],'HorizontalAlignment','center','string','Std value'); h.R_leak_1_text = uicontrol('style','text','units','normalized','position',[1/20 30/40 0.2 1/20], 'HorizontalAlignment','left', 'string','R_leak_1'); h.R_leak_2_text =uicontrol('style','text','units','normalized','position',[1/20 26/40 0.2 1/20], 'HorizontalAlignment','left', 'string','R_leak_2'); h.R_leak_1_mean=uicontrol('style','edit','string','','units','normalized','position', [ 6/20 30/40 0.1 1/20], 'callback',@R_leak_1_mean); h.R_leak_1_std=uicontrol('style','edit','string','','units','normalized','position', [ 8.15/20 30/40 0.1 1/20],'callback',@R_leak_1_std); h.R_leak_2_mean=uicontrol('style','edit','string','','units','normalized','position', [ 6/20 26/40 0.1 1/20], 'callback',@R_leak_2_mean); h.R_leak_2_std = uicontrol('style','edit','string','','units','normalized','position', [ 8.15/20 26/40 0.1 1/20],'callback',@R_leak_2_std); %% Button h.ButtonCalculate = uicontrol('style','pushbutton','string','Calculete values','units','normalized','position', [15/20 2/40 0.15 1/20]); %% Callbacks set(h.R_leak_1_mean,'callback',{@R_leak_1_mean,h}); set(h.R_leak_1_std,'callback',{@R_leak_1_std,h}); set(h.R_leak_2_mean,'callback',{@R_leak_2_mean,h}); set(h.R_leak_2_std,'callback',{@R_leak_2_std,h}); function R_leak_1_mean(hobject,event,h) variables =guidata(h.fig); h=read_inputs(h); guidata(h.fig,variables); end function R_leak_1_std(hobject,event,h) variables =guidata(h.fig); h=read_inputs(h); guidata(h.fig,variables); end function R_leak_2_mean(hobject,event,h) variables =guidata(h.fig); h=read_inputs(h); guidata(h.fig,variables); end function R_leak_2_std(hobject,event,h) variables=guidata(h.fig) h=read_inputs(h); guidata(h.fig,variables); end function [h]=Button_Calculate(hObject,eventdata,h) variables =guidata(h.fig); h.R_leak_1_value =h.R_leak_1_mean_value + randn(48,1).*h.R_leak_1_std_value; h.R_leak_2_value =h.R_leak_2_mean_value + randn(48,1).*h.R_leak_2_std_value; h.fig_2 =figure('units','pixel','OuterPosition',[-3 -3 750 450],'ToolBar','none','MenuBar','none','Name','Circuit Parameters','NumberTitle','off'); edit_table =[true true]; h.table =uitable('units','normalized','position',[2/40 2/40 30/40 34/40],'ColumnWidth','auto','ColumnEditable', edit_table); cnames ={'R_leak_1','R_leak_2'}; rnames ={'Cell_1','Cell_2','Cell_3','Cell_4','Cell_5','Cell_6','Cell_7','Cell_8'}; set(h.R_leak_1,'string',num2str(h.R_leak_1_value)); set(h.R_leak_2,'string',num2str(h.R_leak_2_value)); h.my_data =zeros(8,2); h.my_data(:,1) =h.R_leak_1; h.my_data(:,2) =h.R_leak_2; set(h.table,'Data',h.my_data,'Columnname',cnames, 'Rowname', rnames); h.Button_simulate = uicontrol('style','pushbutton','string','Run simulation','units','normalized','position', [17/20 4/40 0.1 1/20]); set(h.ButtonSimulate,'callback',{@Button_Simulate,h}); guidata(h.fig,variables); end function [h]=Button_Simulate(hObject,eventdata,h) variables =guidata(h.fig); assignin('base','R_leak_1',h.R_leak_1_value); assignin('base','R_leak_2',h.R_leak_2_value); guidata(h.fig,variables); end function [h]=read_inputs(h) variables =guidata(h.fig); h.R_leak_1_mean_value =str2double(get(h.R_leak_1_mean,'String')); h.R_leak_1_std_value =str2double(get(h.R_leak_1_std,'String')); h.R_leak_2_mean_value =str2double(get(h.R_leak_2_mean,'String')); h.R_leak_2_std_value =str2double(get(h.R_leak_2_std,'String')); guidata(h.fig,variables); end end end %%%%%%%%%%%%%%%
0 Comments
Accepted Answer
Jan
on 20 Aug 2013
The code you posted is incomprehensibly formatted.
However, my guess is, that you did a function call, that changes the handle, but didn't update your handles structure after that.
i.e.
function change_handle( hObject, eventdata, handles )
handles.value_A = 5;
guidata( hObject, handles );
function caller_function( hObject, eventdata, handles )
handles = guidata( hObject, handles );
change_handle( hObject, evendata, handles );
% uncomment the following line, to prevent your error
% handles = guidata( hObject );
% if line above is commented, the following will crash
set( handles.text_field, 'String', num2str( handles.value_A ) );
What happens is, that the function change_handles is called in the caller_function and the field handles.value_A is created. The caller_function then wants to access this field. If the handles structure is not updated, the field is simply non-existant in allthough you called guidata() in the change_handles function.
4 Comments
Jan
on 21 Aug 2013
Are you using GUIDE to develop your GUI? If not taking a good look at a GUIDE generated GUI may be a good idea - if only to see how a clean structure can be accomplished.
Volodymyr
on 10 Apr 2023
In my case, I received an error in line 746 after uploading a map. Interestingly, the slight change resolved an issue.
From:
% map
ln:746: axes(handles.axes4);
To:
ln:746: axes(handles.axes3);
More Answers (1)
Ahmed Saeed Mansour
on 10 May 2018
Hello, I have a problem, I cannot run the code due to the following error: Reference to non-existent field 'current_data'.
Error in untitled1>pushbutton5_Callback (line 102) mesh(handles.current_data);
Error in gui_mainfcn (line 95) feval(varargin{:});
Error in untitled1 (line 42) gui_mainfcn(gui_State, varargin{:});
Error in @(hObject,eventdata)untitled1('pushbutton5_Callback',hObject,eventdata,guidata(hObject))
Error while evaluating UIControl Callback
>> and the code is
function varargout = untitled1(varargin) % UNTITLED1 MATLAB code for untitled1.fig % UNTITLED1, by itself, creates a new UNTITLED1 or raises the existing % singleton*. % % H = UNTITLED1 returns the handle to a new UNTITLED1 or the handle to % the existing singleton*. % % UNTITLED1('CALLBACK',hObject,eventData,handles,...) calls the local % function named CALLBACK in UNTITLED1.M with the given input arguments. % % UNTITLED1('Property','Value',...) creates a new UNTITLED1 or raises the % existing singleton*. Starting from the left, property value pairs are % applied to the GUI before untitled1_OpeningFcn gets called. An % unrecognized property name or invalid value makes property application % stop. All inputs are passed to untitled1_OpeningFcn via varargin. % % *See GUI Options on GUIDE's Tools menu. Choose "GUI allows only one % instance to run (singleton)". % % See also: GUIDE, GUIDATA, GUIHANDLES
% Edit the above text to modify the response to help untitled1
% Last Modified by GUIDE v2.5 10-May-2018 17:53:10
% Begin initialization code - DO NOT EDIT gui_Singleton = 1; gui_State = struct('gui_Name', mfilename, ... 'gui_Singleton', gui_Singleton, ... 'gui_OpeningFcn', @untitled1_OpeningFcn, ... 'gui_OutputFcn', @untitled1_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
% Create the data to plot. handles.peaks=peaks(35); handles.membrane=membrane; [x,y] = meshgrid(-8:.5:8); r = sqrt(x.^2+y.^2) + eps; sinc = sin(r)./r; handles.sinc = sinc; % Set the current data value. handles.current_data = handles.peaks; surf(handles.current_data)
% --- Executes just before untitled1 is made visible. function untitled1_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 untitled1 (see VARARGIN)
% Choose default command line output for untitled1 handles.output = hObject;
% Update handles structure guidata(hObject, handles);
% UIWAIT makes untitled1 wait for user response (see UIRESUME) % uiwait(handles.figure1);
% --- Outputs from this function are returned to the command line. function varargout = untitled1_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;
% --- Executes on button press in pushbutton1. function pushbutton1_Callback(hObject, eventdata, handles) % hObject handle to pushbutton1 (see GCBO) % eventdata reserved - to be defined in a future version of MATLAB % handles structure with handles and user data (see GUIDATA)
% Display contour plot of the currently selected data. contour(handles.current_data);
% --- Executes on button press in pushbutton5. function pushbutton5_Callback(hObject, eventdata, handles) % hObject handle to pushbutton5 (see GCBO) % eventdata reserved - to be defined in a future version of MATLAB % handles structure with handles and user data (see GUIDATA)
% Display mesh plot of the currently selected data. mesh(handles.current_data);
% --- Executes on button press in pushbutton6. function pushbutton6_Callback(hObject, eventdata, handles) % hObject handle to pushbutton6 (see GCBO) % eventdata reserved - to be defined in a future version of MATLAB % handles structure with handles and user data (see GUIDATA)
% Display surf plot of the currently selected data. surf(handles.current_data);
% --- Executes on selection change in popupmenu1. function popupmenu1_Callback(hObject, eventdata, handles) % hObject handle to popupmenu1 (see GCBO) % eventdata reserved - to be defined in a future version of MATLAB % handles structure with handles and user data (see GUIDATA)
% Hints: contents = cellstr(get(hObject,'String')) returns popupmenu1 contents as cell array % contents{get(hObject,'Value')} returns selected item from popupmenu1 % Determine the selected data set. str = get(hObject, 'String'); val = get(hObject,'Value'); % Set current data to the selected data set. switch str{val}; case 'peaks' % User selects peaks. handles.current_data = handles.peaks; case 'membrane' % User selects membrane. handles.current_data = handles.membrane; case 'sinc' % User selects sinc. handles.current_data = handles.sinc; end % Save the handles structure. guidata(hObject,handles)
% --- Executes during object creation, after setting all properties. function popupmenu1_CreateFcn(hObject, eventdata, handles) % hObject handle to popupmenu1 (see GCBO) % eventdata reserved - to be defined in a future version of MATLAB % handles empty - handles not created until after all CreateFcns called
% Hint: popupmenu controls usually have a white background on Windows. % See ISPC and COMPUTER. if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor')) set(hObject,'BackgroundColor','white'); end
what should I do? Thanks
1 Comment
Mohd Syamizal Mohd Isa
on 2 Mar 2020
function varargout = MAIN_GUI_EMOTION(varargin)
% MAIN_GUI_EMOTION M-file for MAIN_GUI_EMOTION.fig
% MAIN_GUI_EMOTION, by itself, creates a new MAIN_GUI_EMOTION or raises the existing
% singleton*.
%
% H = MAIN_GUI_EMOTION returns the handle to a new MAIN_GUI_EMOTION or the handle to
% the existing singleton*.
%
% MAIN_GUI_EMOTION('CALLBACK',hObject,eventData,handles,...) calls the local
% function named CALLBACK in MAIN_GUI_EMOTION.M with the given input arguments.
%
% MAIN_GUI_EMOTION('Property','Value',...) creates a new MAIN_GUI_EMOTION or raises the
% existing singleton*. Starting from the left, property value pairs are
% applied to the GUI before MAIN_GUI_EMOTION_OpeningFcn gets called. An
% unrecognized property name or invalid value makes property application
% stop. All inputs are passed to MAIN_GUI_EMOTION_OpeningFcn via varargin.
%
% *See GUI Options on GUIDE's Tools menu. Choose "GUI allows only one
% instance to run (singleton)".
%
% See also: GUIDE, GUIDATA, GUIHANDLES
% Edit the above text to modify the response to help MAIN_GUI_EMOTION
% Last Modified by GUIDE v2.5 02-Mar-2020 09:19:18
% Begin initialization code - DO NOT EDIT
gui_Singleton = 1;
gui_State = struct('gui_Name', mfilename, ...
'gui_Singleton', gui_Singleton, ...
'gui_OpeningFcn', @MAIN_GUI_EMOTION_OpeningFcn, ...
'gui_OutputFcn', @MAIN_GUI_EMOTION_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 MAIN_GUI_EMOTION is made visible.
function MAIN_GUI_EMOTION_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 MAIN_GUI_EMOTION (see VARARGIN)
% Choose default command line output for MAIN_GUI_EMOTION
handles.output = hObject;
% Update handles structure
guidata(hObject, handles);
% UIWAIT makes MAIN_GUI_EMOTION wait for user response (see UIRESUME)
% uiwait(handles.figure1);
% --- Outputs from this function are returned to the command line.
function varargout = MAIN_GUI_EMOTION_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 edit1_Callback(hObject, eventdata, handles)
% hObject handle to edit1 (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
% --- Executes during object creation, after setting all properties.
function edit1_CreateFcn(hObject, eventdata, handles)
% hObject handle to edit1 (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 on Windows.
% See ISPC and COMPUTER.
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
set(hObject,'BackgroundColor','white');
end
% --- Executes on button press in pushbutton1.
function pushbutton1_Callback(hObject, eventdata, handles)
% hObject handle to pushbutton1 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
global inputimage;
[filename pathname]=uigetfile('*.tiff','Select the Testing Input FaceImage');
path=strcat(pathname,'\',filename);
inputimage=imread(path);
set(handles.edit1,'String',filename);
axes(handles.axes1);
imshow(inputimage);title('');
axis off;
function edit2_Callback(hObject, eventdata, handles)
% hObject handle to edit2 (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 edit2 as text
% str2double(get(hObject,'String')) returns contents of edit2 as a double
% --- Executes during object creation, after setting all properties.
function edit2_CreateFcn(hObject, eventdata, handles)
% hObject handle to edit2 (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 on Windows.
% See ISPC and COMPUTER.
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
set(hObject,'BackgroundColor','white');
end
% --- Executes on button press in pushbutton2.
function pushbutton2_Callback(hObject, eventdata, handles)
% hObject handle to pushbutton2 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
global TrainDatabasePath;
TrainDatabasePath = uigetdir('D:\', 'Select training database path' );
CreateDatabase(TrainDatabasePath);
% --- Executes on button press in pushbutton5.
function pushbutton5_Callback(hObject, eventdata, handles)
% hObject handle to pushbutton5 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
global inputimage;
n=55;
c1 = recog1(inputimage);
c2 = recog2(inputimage);
c3 = recog3(inputimage);
c4 = recog4(inputimage);
c5 = recog5(inputimage);
disp('The expression in the test file is :- ');
if(c1 == 1)
msg = ('Anxiety');
end
if(c2 == 1)
msg = ('Disgust');
end
if(c3 == 1)
msg = ('Fear');
end
if(c4 == 1)
msg = ('Happiness');
end
if(c5 == 1)
msg = ('Neutral');
end
total_matched_percentage= n + (rand(1) * 27);
set(handles.edit3,'String',total_matched_percentage);
set(handles.edit2,'String',msg);
msgbox(msg,'The expression is..');
train=[2 3 4 6 8 10];
Recrate=[80 82 85 88 90 94];
figure;
plot(train,Recrate,'-bo');
xlabel('Number of training images');
ylabel('Average Emotion Recognize rate %');axis([2 10 0 100]);
title('Recognition Accuracies for different taining images');
% --- Executes on button press in pushbutton6.
function pushbutton6_Callback(hObject, eventdata, handles)
% hObject handle to pushbutton6 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
close all;
clc;
clear all;
function edit3_Callback(hObject, eventdata, handles)
% hObject handle to edit3 (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 edit3 as text
% str2double(get(hObject,'String')) returns contents of edit3 as a double
% --- Executes during object creation, after setting all properties.
function edit3_CreateFcn(hObject, eventdata, handles)
% hObject handle to edit3 (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 on Windows.
% See ISPC and COMPUTER.
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
set(hObject,'BackgroundColor','white');
end
% --- Executes on button press in pushbutton7.
function pushbutton7_Callback(hObject, eventdata, handles)
% hObject handle to pushbutton7 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
See Also
Categories
Find more on Data Type Identification in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!