MATLAB: Error while evaluating uicontrol Callback (GUI_handle problem)

guireference to non-existent field error

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 %%%%%%%%%%%%%%%

Best Answer

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.
Related Question