Matlab checkbox gui - matlab

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/

Related

Expression not evaluated at callback statement

I am building a GUI in Matlab that plots different functions in different plots according to the selected RadioButton. It looks like:
I want to select at which axes should the function be plotted. Therefore I need to know which of the RadioButton is selected. Then:
% Set default RadioButton
set(h.r1,'Value',1);
set(h.r2,'Value',0);
set(h.r1,'Callback',{#myRadioButton, h.r2}); % Set the other RadioButton to false
set(h.r2,'Callback',{#myRadioButton, h.r1}); % Set the other RadioButton to false
Then I call press one of any Button and call my plotting function which (should) evaluate(s) which axes it has to use:
set(h.b1,'Callback',{#myPlotFunction,...
get(h.r1,'value')*h.axes1+get(h.r2,'value')*h.axes2, h.x});
The problem is that I always get h.axes1 as input, no matter which RadioButton I choose.
Is this due to the definition of default values for RadioButton?
EDIT
Another edit: Ok, now we're dynamically assigning the button handles and xdata inside of the radiobutton callback.
Due to some peculiarities with MATLABs handling of function handle arguments (see comments), my first answer doesn't work. This one does.
set(h.r1,'Callback',{#TurnOffRadio, h.r2, h.axes1, h.x});
set(h.r2,'Callback',{#TurnOffRadio, h.r1, h.axes2, h.x});
function TurnOffRadio(hObject, eventdata, radiohandle, axeshandle, xdata)
set(radiohandle, 'Value', 0);
set(h.b1,'Callback',{#myPlotFunction, axeshandle, xdata});
%// Repeat set statement for each button.
end %// You may nee to remove this end, depending on your conventions.
function myPlotFunction(hObject, eventdata, axeshandle, xdata)
%// do whatever here with axeshandle and xdata.
end %// You may nee to remove this end, depending on your conventions.
I've set up a toy example and run this exact code, and it produces the desired effect.
Let me know if this still doesn't solve the problem, and I'll keep digging.
Original
You're close. The problem is that when you set a callback function using a function handle and arguments, the arguments always remain what they were at the time that the callback was set. Essentially, they are passed only once, not dynamically each time the callback is executed. To work around this, just set the callback to turn off the other radio button directly using set.
set(h.r1,'Callback',{#set, h.r2, 'Value', 0});
set(h.r2,'Callback',{#set, h.r1, 'Value', 0});
Let me know if that doesn't work for you. Good luck!

Matlab adding KeyListener to existing button

I've written a Matlab program which counts different values when are particular button is pressed (so far just the numbers of "yes" and "no"). I'm trying to add a key listener so that when I press, for example, n on the keyboard the button is pressed, or the same actions are completed. I have tried the addListener and keyfunclistener functions but neither seems to be working.
Here is an example of the button:
no=uicontrol(h_fig,'callback',countnonerve,'position',[.65 .07 .1 .08],'string','No','style','pushbutton','Units','normalized');
Any suggestions? It would be very helpful I'm not familiar with MatLab
You could try using the KeyPressFcn property of the figure to record/capture individual key presses:
function keypress_Test
h = figure;
set(h,'KeyPressFcn',#keyPressCb);
function keyPressCb(src,evnt)
disp(['key pressed: ' evnt.Key]);
end
end
The above code can be pasted into a file and saved as keypress_Test.m. A figure is created and a callback assigned to the KeyPressFcn property.
It can be easily adapted for your GUI. Wherever you have access to the figure handles (probably the h_fig from above) just add set(h_fig,'KeyPressFcn',#keyPressCb); and paste the keyPressCb code into the same file.
If you are counting the number of times a certain key is pressed, then you will have to save this information somewhere..probably to the handles structure. Is that the case? If so, then you can easily access this from the callback
function keyPressCb(src,evnt)
disp(['key pressed: ' evnt.Key]);
% get the handles structure
handles = guidata(src);
if ~isempty(handles)
% do something here - increment count for evnt.Key
% save the updated count
guidata(src,handles);
end
end
Try it out and see what happens!

turn on colorbar programmatically in clustergram

I know that one can insert a colorbar by clicking the colorbar icon in the clustergram GUI. Is there a way to do it programmatically?
I tried
cgo = clustergram(data)
colorbar;
This makes a colorbar in a new figure window. How can a colorbar be created with proper positioning in a clustergram figure as if the button was clicked?
There is a function buried away (HeatMap.plot>showColorbar) that neatly positions the colorbar to the left of both the heat map and the dendogram (the lines). Just running colorbar(...) will mess up the relative positioning of the dendogram and the heatmap. So you need to somehow run the callback or carefully duplicate all of the position computations. It's easier to just run the callback. Here's how.
To create the colorbar programmatically for a clustergram, and keep the color bar button in sync, you need to use the button's assigned callback and set the button's state.
Create the clustergram:
load filteredyeastdata
cgo = clustergram(yeastvalues(1:30,:),'Standardize','Row');
Get the handle for color bar button:
cbButton = findall(gcf,'tag','HMInsertColorbar');
Get callback (ClickedCallback) for the button:
ccb = get(cbButton,'ClickedCallback')
ccb =
#insertColorbarCB
[1x1 clustergram]
That gives us a handle to the function assigned by the callback (#insertColorbarCB), and the function's third input argument (the clustergram object). The button's handle and an empty event object are implicitly the first two arguments.
Change the button state to 'on' (clicked down):
set(cbButton,'State','on')
Run the callback to create the colorbar:
ccb{1}(cbButton,[],ccb{2})
Note that the button State must be changed to 'on' first, otherwise the callback won't do anything.
I just managed to solve this problem.
What I did:
I added this function to the clustergram code (I put it at line 1486)
%%%%%%%%%%%%%%
function insertColorbarCBALWAYS(obj)
hFig= gcbf;
obj.Colorbar = true;
end
%%%%%%%%%%%%%%%
and then at line 415 of the clustergram.m file I added this line of code
insertColorbarCBALWAYS(obj);
to call the above function. Save and go: now the colorbar will always be there, once the clustergram is drawn.
Previous method was not working for me so I made this workaround.
One may even save the new clustergram code as clustergramCM such that you can draw cgram in both ways.

How can I save the edit box data in the GUI Guide?

I am making a GUI in which there is a multi-line edit box.
the user will have to input the 3 x-y coordinates in this edit box at a time:
[345.567 123.123]
[390.567 178.098]
[378.000 125.987]
by clicking the push button I want these coordinates to be "saved" in the Matlab GUI workspace in the form of a matrix and by clicking another push button "reloaded" from the workspace so that they can be available for future use.
How can I do that?
Can anyone guide me with this? Help would be appreciated!
There are a number of ways to manage data in GUIDE-generated GUIs. The easiest IMO is to use guidata.
For example inside the "Save" push button callback, you would access the edit box string contents, parse as matrix of numbers as save it inside the handles structure.
function pushbuttonSave_Callback(hObject, eventdata, handles)
handles.M = str2num(get(handles.edit1, 'String'));
guidata(hObject, handles);
end
Next in the "load" button, we do the opposite, by loading the matrix from the handles structure, converting it to string, and set the edit box content:
function pushbuttonLoad_Callback(hObject, eventdata, handles)
s = num2str(handles.M, '%.3f %.3f\n');
set(handles.edit1, 'String',s)
end
If you want to export/import the data to/from the "workspace", you can use the ASSIGNIN/EVALIN function:
assignin('base','M',handles.M);
and
handles.M = evalin('base','M');
to save data:
setappdata(h,'name',value)
to load data:
value = getappdata(h,'name')
values = getappdata(h)
where h is the handle you store the data in, name is the variable of the data, value is the actual data.

Add a button on a figure and to close the figure in MATLAB

I have a set of data that i have pulled out of the database. I have displayed them in a figure window, but i would like to have a button in which where it closes the figure window or does some other function to it.
This is the code that i have so far :
f = figure('Position',[200 200 250 500]); % size of the figure object
dat = listofPdb.Data;
set(f,'name','List of PDBs available','numbertitle','off') %renames the Title Figure
cnames = {'PDB-Codes'};
rnames = {};
t = uitable('Parent',f,'Data',dat,'ColumnName',cnames,...
'RowName',rnames,'Position',[100 100 95 350]);
Please advise.
You need to define CloseRequestFcn property of the figure:
set(f,'CloseRequestFcn', #closereq)
where closereq is a function what to do when figure is closed.
See Figure properties for more information and examples.
Update (after chat in comments):
For a pushbutton you can define the callback function just to close the figure (insert close(get(hObject,'Parent')) into pushbutton1_Callback) and the CloseRequestFcn will do the rest.
On the other hand, if you want the pushbutton to do something before closing the figure, but don't want to do it with standard closing, then just insert those actions to the pushbutton callback, not to CloseRequestFcn.
Type guide and design your figure. Than place a pushbutton over it, right click -> closing function. And define the behaviour you want to have for closing the figure.