How to pass handles into certain GUI callbacks Matlab - matlab

I'm writing a matlab program and I'm trying to pass around my handles struct to ALL my callbacks. The only problem is that I'm using GUIDE and I can't pass in handles as an argument to a function I created:
%The proper slider callback: Deleted the default within GUIDE
function sliderContValCallback(hFigure,eventdata)
%test it out- get the handles object and dispay the current value
%getappdata(handles.video_loader_button,'handles');
handles = guidata(hFigure);
handles.currentFrame = floor(get(handles.slider,'Value'));
set(handles.frameBox,'String',num2str(handles.currentFrame));
fprintf('slider value: %f\n',get(handles.slider,'Value'));
updateAxes(handles);
The problem is that the way I'm 'getting' handles right now is that it isn't really the same handles that the rest of the UI object callback functions are using. I also thought of passing handles around with getappdata, but you need handles to even do that so I'm stuck. This has been causing some problems, any help as to how to get around this would be awesome, thanks!
EDIT:
I deleted the call back generated by GUIDE, so that I could use sliderContValCallback as a function handle and call it here:
handles.sliderListener = addlistener(handles.slider,'ContinuousValueChange',...
#(hFigure,eventdata) sliderContValCallback(hObject,eventdata));
for continuous updates as the user drags the slider(This is used to scroll through a video, and its working well).
The real reason I'm questioning this is because when I call:
allCoords = getappdata(handles.axes1,'mydata');
coordsXYZ = allCoords.clickcoordinates; %Do this to access the field 'allCoords' within 'mydata'
curIndex = allCoords.currentIndex;
if numel(coordsXYZ)>0
for i = 1:length(coordsXYZ)
coordXY = [coordsXYZ(i).x,coordsXYZ(i).y];
%viscircles(handles.axes1,coordXY,1,'EdgeColor','r');
hold on;
plot(coordsXYZ(i).x,coordsXYZ(i).y,'o');
end
end
everything works but a figure is generated for some reason, the plot is drawn to an image on handles.axes1. It's weird because this random figure pops up ONLY when I update the slider by moving it.

Related

Matlab Get Handle for Layout

As the title states, I need to get a handle for my Matlab application. My class is derived from matlab.apps.AppBase and is app.UIFigure (if that matter, I'm still learning Matlab). My main goal is to change the mouse cursor to watch after a button is clicked and data is processed in the background.
I have tried:
set(gcf,'Pointer','watch')
But gcf is just empty, so it creates a new figure. I have also gotten all of the figures, using:
figs = findall(groot,'Type','Figure')
Which finds all of the figures I am using. I believe that I need to get the overall application figure and find the handle, but I am unsure how to do that.
There is no pointer property for uifigure; otherwise, you would be able to use app.UIFigure.Pointer = 'watch' as suggested by #CrisLuengo.
However, specially for uifigure MATLAB provides a nice looking and powerful progress bar uiprogressdlg. You can make it indeterminate with uiprogressdlg.Indeterminate = on;. I find this working pleasingly well.
Here is an example:
f=uifigure;
progressdlg=uiprogressdlg(f,'Title','Progress','Message', 'Doing something please wait', 'Indeterminate','on');
pause(10); % Run your algorithm.
% Delete the progress bar after work done.
progressdlg.delete();

Push button is only changing position once

I have a question about callback functions in MATLAB's GUIDE. I have the following code execute when a button is pushed:
handles.xPos=handles.xPos+1
addX = handles.xPos
handles.shape2 =fill ([-2+addX 1+addX 1+addX -1+addX], [1 1 -1 -1], 'r');
This works, but only once (and the old shape is still there, but that is a separate problem). I have done debugging code and have determined that the callback function is always called when the button is pushed, but for some strange reason there is no effect in the change of the position after the first push of the button.
What am I doing wrong here?
You have to update your handles via guidata to take the modifications of handles into account:
guidata(hObject,handles);
Otherwise the modifications of handles are lost at the end of the callback's execution.
Best

Matlab GUI:Setting Current Axes creates a new figure

I have created a GUI with 3 axes: axes1, axes2, axes3. I have a class SP to whose constructor I pass the three axes as follows:
a=SP(handles.axes1,handles.axes2,handles.axes3)
The class looks something like
class SP < handles
properties
axes1
axes2
axes3
end
methods
function A=SP(axes1,axes2,axes3)
A.axes1=axes1;
A.axes2=axes2;
A.axes3=axes3;
axes(A.axes1);
rectangle('Position',[randn,randn,randn,randn]);
axes(A.axes2);
rectangle('Position',[randn,randn,randn,randn]);
axes(A.axes3);
rectangle('Position',[randn,randn,randn,randn]);
end
I have written a timer function
function timerfcn1(~,~,A)
axes(A.axes1);
rectangle('Position',[randn,randn,randn,randn]);
axes(A.axes2);
rectangle('Position',[randn,randn,randn,randn]);
axes(A.axes3);
rectangle('Position',[randn,randn,randn,randn]);
end
My problem is that during the initialization i.e when i call the constructor, the rectangles are plotted in the GUI window. However, whenever the timerfcn1 runs it creates a new figure and plots the rectangle in that.
I had used a similar thing before and it used to work then.
Most likely it's related to handle visibility (or lack of) in a part of the code that you haven't shown. To guarantee that the rectangle goes to the axis you want use,
rectangle('Position',[...],'Parent',A.axes1)
Before this you should also check that the handle still exists using
if ishandle(A.axes1)
...
end

Matlab checkbox gui

I have a checkbox on GUI that draws a rectangle on a live video feed, however, I need the rectangle to dissapear or be deleted when I uncheck it.
does anyone have any idea how to do this?
This is my code, I have tried putting things in else, but nothing works.
function Box(hObject,eventdata)
if (((get(hObject,'Value') == get(hObject,'Max'))))
% Checkbox is checked-take appropriate action
hold on;
rectangle('Position',[50,50,100,100],'EdgeColor','r')
else
end
You need to save the handle created by the function rectangle. Then add this handle to the big handle of your GUI so that you are able to have access to it once the callback is called again.
So modify your function like so
function Box(hObject,eventdata,handles)
if (((get(hObject,'Value') == get(hObject,'Max'))))
% Checkbox is checked-take appropriate action
hold on;
handles.rectangleSave=rectangle('Position',[50,50,100,100],'EdgeColor','r');
guidata(handles.output,handles);
else
delete(handles.rectangleSave);
end
If you have never used handles, please have a look here :
http://www.matlabtips.com/on-handles-and-the-door-they-open/
handles.output usually stores the handle to the big interface window as explained here :
http://www.matlabtips.com/guide-me-in-the-guide/

Does Matlab execute a callback when a plot is zoomed/resized/redrawn?

In Matlab, I would like to update the data plotted in a set of axes when the user zooms into the plot window. For example, suppose I want to plot a particular function that is defined analytically. I would like to update the plot window with additional data when the user zooms into the traces, so that they can examine the function with arbitrary resolution.
Does Matlab provide hooks to update the data when the view changes? (Or simply when it is redrawn?)
While I have yet to find one generic "redraw" callback to solve this question, I have managed to cobble together a group of four callbacks* that seem to achieve this goal in (almost?) all situations. For a given axes object ax = gca(),
1. Setup the zoom callback function as directed by #Jonas:
set(zoom(ax),'ActionPostCallback',#(x,y) myCallbackFcn(ax));
2. Setup a pan callback function:
set(pan(ax),'ActionPostCallback',#(x,y) myCallbackFcn(ax));
3. Setup a figure resize callback function:
set(getParentFigure(ax),'ResizeFcn',#(x,y) myCallbackFcn(ax));
4. Edit: this one no longer works in R2014b, but is only needed if you add, e.g., a colorbar to the figure (which changes the axis position without changing the figure size or axis zoom/pan). I've not looked for a replacement. Finally, setup an undocumented property listener for the axes position property itself. There is one important trick here: We must hold onto the handle to the handle.listener object as once it's deleted (or leaves scope), it removes the callback. The UserData property of the axes object itself is a nice place to stash it in many cases.
hax = handle(ax);
hprop = findprop(hax,'Position');
h = handle.listener(hax,hprop,'PropertyPostSet',#(x,y) myCallbackFcn(ax));
set(ax,'UserData',h);
In all these cases I've chosen to discard the default callback event arguments and instead capture the axis in question within an anonymous function. I've found this to be much more useful than trying to cope with all the different forms of arguments that propagate through these disparate callback scenarios.
*Also, with so many different callback sources flying around, I find it invaluable to have a recursion check at the beginning of myCallbackFcn to ensure that I don't end up in an infinite loop.
Yes, it does. The ZOOM mode object has the following callbacks:
ButtonDownFilter
ActionPreCallback
ActionPostCallback
The latter two are executed either just before or just after the zoom function. You could set your update function in ActionPostCallback, where you'd update the plot according to the new axes limits (the handle to the axes is passed as the second input argument to the callback).