Attempt to reference field of non-structure array error occurs while using next button

I have the following Matlab GUI code for pair copmarison of images on pressing next button it should change the image but it gives the error that "Attempt to reference field of non-structure array." The code is following
% --- Executes just before GUI_Personality_Impressions is made visible.
function GUI_Personality_Impressions_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 GUI_Personality_Impressions (see VARARGIN)
% Choose default command line output for GUI_Personality_Impressions
handles.output = hObject;
% Update handles structure
guidata(hObject, handles);
% UIWAIT makes GUI_Personality_Impressions wait for user response (see UIRESUME)
% uiwait(handles.figure1);
% --- Outputs from this function are returned to the command line.
function varargout = GUI_Personality_Impressions_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)
set(handles.axes1,'units','pixels');
set(handles.axes2,'units','pixels');
scrz=get(0,'ScreenSize')
% pos2=[(scrz(3)-800)/2 (scrz(4)-600)/2 800 600];
fig_hr = 326;
fig_vr = 493;
pos1 = round((scrz(3)-fig_hr)/4);
pos2 = round((scrz(4)-fig_vr)/2);
handles.pos1 = pos1;
handles.pos2 = pos2;
% fig_xcoord = (ScreenSize(3) - fig_width)/2;
handles.pos3 = [pos1 pos2 fig_hr fig_vr];
set(handles.axes1,'pos',[handles.pos3]);
axes(handles.axes1);
imshow('1.tif');
% pos1 = round((scrz(3)-fig_hr)/ 3)
posa = pos1 +1.5* round(fig_hr);
pos4 = [posa pos2 fig_hr fig_vr]
set(handles.axes2,'pos',[pos4]);
axes(handles.axes2);
imshow('2.tif');
% myui
% % Get default command line output from handles structure
varargout{1} = handles.output;
handles.co = 1.
for i =1:43*2
handles.save_img{i} = imread ([num2str(i),'.tif']);
end
%%Radio button and next button
hBtnGrp = uibuttongroup('Position',[ 0 0 0.1 0.1], 'Units','Normalized');
uicontrol('Style','Radio', 'Parent',hBtnGrp, 'HandleVisibility','off', 'Position',[(pos1+326+pos1)/2, pos2-70,70 ,50],'Value',0, 'String','A', 'Tag','A')
uicontrol('Style','Radio', 'Parent',hBtnGrp, 'HandleVisibility','off','Position' ,[(posa+326+posa)/2, pos2-70,70 ,50],'Value',0, 'String','B', 'Tag','B')
% uicontrol('Style', 'pushbutton','Callback', @pushbutton1,'Units', 'pixels','Position', [(((pos1+326+pos1)/2)+(posa+326+posa)/2)/2, pos2- 140,70 ,50 ],'String','Next');
uicontrol('Style', 'pushbutton','Callback', {@pushbutton1, hBtnGrp}, 'Units', 'pixels', 'Position', [(((pos1+326+pos1)/2)+(posa+326+posa)/2)/2, pos2- 140,70 ,50 ], 'String', 'Next');
function pushbutton1(hObject,handles,hBtnGrp)
global data
switch get(get(hBtnGrp,'SelectedObject'),'Tag')
case 'A', data = 1;
imshow(handles.save_img{handles.co},'Parent',handles.axes1)
handles.co = handles.co + 1;
case 'B', data = 2;
imshow(handles.save_img{co},'Parent',handles.axes2)
handles.co = handles.co + 1;
end

 採用された回答

Walter Roberson
Walter Roberson 2016 年 3 月 15 日

0 投票

You are not passing handles to your pushbutton1 . You have named the second parameter of that function "handles", but the second parameter of a callback is always the "event" parameter. So your code is looking at the event data that is passed in automatically and is trying to treat it as if it was the handles data structure.

6 件のコメント

Muhammad Farhan  Mughal
Muhammad Farhan Mughal 2016 年 3 月 15 日
Yes you are right, I tried it before posting the question but still it gives error . The issue is for "function pushbutton1(hObject,handles,hBtnGrp)" both handles and hBtnGrp should be at 3rd location which is not possible. So what do you think is the possible solution to overcome this hurdle.
uicontrol('Style', 'pushbutton','Callback', {@pushbutton1, handles, hBtnGrp}, 'Units', 'pixels', 'Position', [(((pos1+326+pos1)/2)+(posa+326+posa)/2)/2, pos2- 140,70 ,50 ], 'String', 'Next');
function pushbutton1(hObject, event, handles, hBtnGrp)
This will have its own problems. So what you should do is,
uicontrol('Style', 'pushbutton','Callback', {@pushbutton1, ancestor(hObject, 'figure'), hBtnGrp}, 'Units', 'pixels', 'Position', [(((pos1+326+pos1)/2)+(posa+326+posa)/2)/2, pos2- 140,70 ,50 ], 'String', 'Next');
function pushbutton1(hObject, event, GUIfig, hBtnGrp)
handles = guidata(GUIfig);
and at the end of the routine
guidata(GUIfig, handles)
Muhammad Farhan  Mughal
Muhammad Farhan Mughal 2016 年 3 月 16 日
編集済み: Muhammad Farhan Mughal 2016 年 3 月 16 日
Thank you it is done
Image Analyst
Image Analyst 2016 年 3 月 16 日
Once again you did not give the complete error message. ALWAYS, paste the entire error message - that means ALL THE RED TEXT.
Muhammad Farhan  Mughal
Muhammad Farhan Mughal 2016 年 3 月 16 日
Thank you for your response @image Analyst. Next time I will be careful but for now the issue is resolved.
I have one more question
function pushbutton1(hObject,handles,hBtnGrp)
global data
switch get(get(hBtnGrp,'SelectedObject'),'Tag')
case 'A', data = 1;
imshow(handles.save_img{handles.co},'Parent',handles.axes1)
handles.co = handles.co + 1;
case 'B', data = 2;
imshow(handles.save_img{co},'Parent',handles.axes2)
handles.co = handles.co + 1;
end
if (handles.co == 83)
cla (handles.axes1); cla (handles.axes2);
close all
end
imshow(handles.save_img{handles.arr(handles.co,1)},'Parent',handles.axes1)
imshow(handles.save_img{handles.arr(handles.co,2)},'Parent',handles.axes2)
guidata(GUIfig, handles)
It gives error when i use close all to close GUI "HAX must be a valid axes handle."

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

ヘルプ センター および File ExchangeData Type Conversion についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by