MATLAB: BytesAvailableFcn and hObject Scope

callbackfigureguidehandlesmatlab guiserial

Hi,
I am using GUIDE, I have a button that opens a serial connection and configures the bytes available function as such:
handles.s.BytesAvailableFcn = {@update_function,hObject};
The callback function is as such:
function update_function(hObject,eventdata,hfigure)
handles = guidata(hfigure);
B=zeros(7,7);
B(:)= single(42.0);
%Set the temp axes
axes(handles.temp_axes);
handles.current_data = B;
image(handles.current_data,'CDataMapping','scaled');
Instead of plotting the data on the existing temp axes, it creates a new figure window and plots it there, so I know somehow I have the scope wrong. Can someone see where I've gone wrong?
Cheers

Best Answer

function update_function(hObject,eventdata,hfigure)
handles = guidata(hfigure);
B=zeros(7,7);
B(:)= single(42.0);
%Set the temp axes

handles.current_data = B;
image(handles.temp_axes, handles.current_data, 'CDataMapping', 'scaled');
guidata(hfigure, handles);
Note that this will clear the axes each time, destroying the colormap information and the tick information and the colorbar. If you do not want that to happen, you should not image(). Instead:
%INITIALISE AXES---------------------------------------------
hfigure = ancestor(handles.temp_axes, 'figure');
A = zeros(7,7);
A(:)= single(37.0);
% Set the temp axes
handles.current_data = A;
handles.ImageH = image(handles.temp_axes, handles.current_data, 'CDataMapping', 'scaled')
axis(handles.temp_axes, 'image')
colorbar(handles.temp_axes);
colormap(handles.temp_axes, jet)
set(handles.temp_axes, 'YTickLabel',[]);
set(handles.temp_axes,'XTickLabel',[]);
guidata(hfigure, handles);
together with
function update_function(hObject,eventdata,hfigure)
handles = guidata(hfigure);
B = zeros(7,7);
B(:)= single(42.0);
%Set the temp axes
handles.current_data = B;
set(handles.ImageH, 'CData', handles.current_data);
guidata(hfigure, handles);
Note that with scaled CData that is all constant value, you will not be able to see the difference between the two.