user6110593
user6110593

Reputation:

unable to store variable in Matlab Gui

I know several variants of this issue have been discussed elsewhere, but I am still unable to solve the problem. Please help.

I have created a function as part of a larger gui, but I am unable to store three data variables (AveX, AveY, AveZ) for later use by guidata(hObject, handles). What am I doing wrong?

Here is the function:

%call all checkbox values
for i = 1:30
checkboxes=get(handles.(sprintf('checkboxAv%d',i)),'value')
Checkboxes(i,1)=checkboxes(1,1);
end

plotdata=handles.plotdata;

[row,col] = find(Checkboxes==1)


num=length(plotdata{1,1}(:,1));
DataY = zeros(num,length(row));%zero matrix

%Average y data
for k=1:length(row)
    DataY(:,k)=plotdata{row(k,1),col(k,1)}(:,4);
end

[m,n] = size(DataY)
if (n==1)
    AveY=DataY'
elseif (n>1)    
    AveY=mean(DataY');
end
AveY=AveY';


%Average X data
for kk=1:length(row)
    DataX(:,kk)=plotdata{row(kk,1),col(kk,1)}(:,1);
end

test=DataX(:,1);
comp=any(bsxfun(@minus,DataX,test),1)
S = sum(comp)
    if (S > 0)
            h=msgbox(['Note! Wavelength index for the selected samples are not identical.'])
    end


[c,r] = size(DataY)
if (r==1)
    AveX=DataX'
elseif (r>1)    
    AveX=mean(DataX');
end
AveX=AveX';


%Average Z data
for kkk=1:length(row)
    DataZ(:,kkk)=plotdata{row(kkk,1),col(kkk,1)}(:,5);
end

[m,n] = size(DataZ)
if (n==1)
    AveZ=DataZ'
elseif (n>1)    
    AveZ=mean(DataZ');
end
AveZ=AveZ';

handles.Aveheader=Aveheader
handles.AveX=AveX;
handles.AveY=AveY;
handles.AveZ=AveZ;
guidata(hObject, handles);

And here is the error message:

Undefined function or variable 'hObject'.

Error in CDanalyzer>AveragePlotFcn (line 5276)
guidata(hObject, handles);

Error in CDanalyzer>checkboxAv1_Callback (line 5076)
AveragePlotFcn(handles)

Error in gui_mainfcn (line 95)
        feval(varargin{:});

Error in CDanalyzer (line 17)
    gui_mainfcn(gui_State, varargin{:});

Error in
matlab.graphics.internal.figfile.FigFile/read>@(hObject,eventdata)CDanalyzer('checkboxAv1_Callback',hObject,eventdata,guidata(hObject)) 
Error while evaluating UIControl Callback

Upvotes: 0

Views: 43

Answers (1)

user5128199
user5128199

Reputation:

"guidata(object_handle,data) stores the variable data with the object specified by object_handle" you need to specify the object_handle. Currently hObject is undefined in this local function.

Use gcbo instead, which "returns the handle of the graphics object whose callback is executing":

guidata(hObject, handles);

becomes

guidata(gcbo, handles);

Alternatively, add hObject as an input to the function AveragePlotFcn. So:

function AveragePlotFcn(hObject,~)
...
end

Upvotes: 1

Related Questions