GUI Can't print result to Static Text or Edit text box! Error message

1 view (last 30 days)
So I have a simple GUI with two push buttons and one Static text box to show the output of the computation. The LoadDataButton push button loads the data and RunAnalysisButton push button runs analysis and displays result. The code is shown below, omitting the presumably irrelevant part of it. The error I am getting is "Attempt to reference field of non-structure array". The error message is long but not sure why I am getting this error since the set command is simple and it has worked for me before. Any help on the issue is appreciated.
% --- Executes on button press in LoadDataButton.
function LoadDataButton_Callback(hObject, eventdata, handles)
% hObject handle to LoadDataButton (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
S = uiimport;
K = S.data;
guidata(hObject,K)
% --- Executes on button press in RunAnalysisButton.
function RunAnalysisButton_Callback(hObject, eventdata, handles)
% hObject handle to RunAnalysisButton (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
f = guidata(hObject);
ti = f(:,1); C = f(:,2); C1 = 1-C;
[Exponential_Fit, ~] = expfit(ti,0.5,C1);
set(handles.edit1,'String',Exponential_Fit)
  3 Comments
Stephen23
Stephen23 on 21 Sep 2015
"The error message is long but not sure why I am getting this error" The error message is long because it give lots of information about the error. If you tell us the complete error message then it helps us understand what the problem is.
Hammad Awan
Hammad Awan on 21 Sep 2015
Here is the complete error message. I am getting error at "set" command. Everything is fine just before that line.
Attempt to reference field of non-structure array.
Error in WeibullAnalysis>RunAnalysisButton_Callback (line 96) set(handles.ExponentialMuField,'String',num2str(Exponential_Fit));
Error in gui_mainfcn (line 95) feval(varargin{:});
Error in WeibullAnalysis (line 42) gui_mainfcn(gui_State, varargin{:});
Error in @(hObject,eventdata)WeibullAnalysis('RunAnalysisButton_Callback',hObject,eventdata,guidata(hObject))
Error while evaluating UIControl Callback

Sign in to comment.

Accepted Answer

Walter Roberson
Walter Roberson on 21 Sep 2015
When you do
K = S.data;
guidata(hObject,K)
You are replacing the entire "handles" array with the content of K, which is not even a structure. http://matlab.wikia.com/wiki/FAQ#How_can_I_share_data_between_callback_functions_in_my_GUI.3F
  3 Comments
Walter Roberson
Walter Roberson on 21 Sep 2015
MATLAB automatically provides only two arguments to callbacks, not three. MATLAB does not automatically provide the "handles" structure.
GUIDE finds it handy to put all kinds of information into a structure and pass that structure into routines as a third argument traditionally known as "handles".
The way that GUIDE does that is it programs the real callback properties for objects so that they do not refer directly to the callback routine you see the code for. The callback that GUIDE puts in fetches the current master copy of the data structure and calls the callback routine that you see in the code, passing in a copy of that data structure as the third argument.
The way that GUIDE fetches the current master copy of the data structure is that it calls guidata(), which gets the information from a property stored in the figure object.
When you call
K = S.data;
guidata(hObject,K)
you are telling MATLAB to replace the entire master copy of the data structure that GUIDE uses, and you are replacing it with your numeric data array.
Then when the next callback is triggered, GUIDE goes and fetches the master copy of the data structure, and finds there the numeric data array, so that numeric data array is what being passed into your second routine in the parameter named handles.
Then you fetch the master copy of the data structure again and assign it to f. Now your function has one copy of the numeric data structure stored in handles (passed in) and another copy of the numeric data structure stored in f. You use the numeric data structure that is in f. Then you try to refer to a field in handles, but because you wrote over the master copy of the data structure in your previous routine, storing your numeric array there, it is your numeric array contents that you attempt to use the edit1 field of. And that of course fails because your numeric array has no fields.
This is why I said 'You are replacing the entire "handles" array with the content of K, which is not even a structure.'.
How should you be transferring data between callbacks? There are several ways, which are described in the link I pointed you to earlier. If you had read the link you would have already been able to correct your code and I would not have had to type out this long description. :(
Hammad Awan
Hammad Awan on 21 Sep 2015
Thanks for the long reply. I have already used the setappdata command from the link you suggested. Needed to understand the guidata well so I asked it.

Sign in to comment.

More Answers (0)

Categories

Find more on Interactive Control and Callbacks 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!