turn on colorbar programmatically in clustergram - matlab

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.

Related

missing figure toolbar and possible change in global variables

Today i tried adding a slider below my graph, so different graphs would be drawn when you slide it. Eventually i got all of this to work perfectly fine, however i still have two small issues.
On top of the figure with the slider, the figure toolbar is gone. It can easily be added by clicking view -> figure toolbar at the top in the figure itself. However to do this everytime is a bit tedious. And when i google'd for the problem, the results where are all guides to create custom toolbars. I just want to add the default toolbar back, it was fine. But i do not know how to do that. Typing 'uitoolbar' gives an empty one. But i am not sure how to fill it with the default icons.
And the second problem i ran into;
The main script calls in the end for a function, this function then creates the new graph with the slider. However herefore i needed to transfer the matrix of data to plot from the main script to the function, so i made the matrix a global variable. However as soon as the function is called, matlab gives me this error (once for each global variable):
> In xxxx at 270
Warning: The value of local variables may have been changed to match the globals.
Future versions of MATLAB will require that you declare a variable to be global before you use that variable.
Before i call the function i type
global xL yL zL p
which are the data required to plot. Then the first line in the function file is this same line again. The data seems to be transmitted fine and the graph shows the correct plot.
Is this some error i can just ignore then? Since everything works as it should? Or what does it mean exactly?
Ok here are my 2 cents:
1) From what I understand the simplest way to fetch the toolbar would be using findobj and then make it visible:
hToolBar = findobj('Type','uitoolbar');
set(hToolBar,'Visible','on');
You can also get the toolbar's properties using "get". Here is what it looks like from a simple script I ran:
get(hToolBar)
hToolBar =
0.1890 %// don't worry about that number; it's the handle for the toolbar.
BeingDeleted = off
ButtonDownFcn =
Children = [1.18896]
Clipping = on
CreateFcn =
DeleteFcn =
BusyAction = queue
HandleVisibility = on
HitTest = on
Interruptible = on
Parent = [1]
Selected = off
SelectionHighlight = on
Tag =
Type = uitoolbar
UIContextMenu = []
UserData = []
Visible = off
I don't know why it disappears though.
2) You might get rid of those global variables if you sent your data as inputs to your function is that a possibility?
For example:
function PlotData(xL,yL,zL,p)
%// Your code here
Hope that helps!

Adding a MATLAB GUI to a MATLAB code

I am asked by my professor to add a GUI for my Matlab code. My program receives an image as an input and returns a string.
The GUI should enable me to browse the image and then display it. Then I need to use that image in the Matlab code.
To browse and display the image, I created a pushbutton control and wrote the following in its callback
[baseFileName, folder] = uigetfile('*.jpg');
fullFileName = [folder baseFileName];
rgbImage = imread(fullFileName,'jpg');
imshow(rgbImage);
I added a second pushbutton and the Matlab code (which has a file name main.m) inside its callback. This function needs the image displayed above as an input, and its output (which is a string) needs to be displayed in the GUI.
I am facing a few problems:
I want the image to be displayed in a specific position.
How can I call the function in the push button?
How can I access and use the image in the first push button to the second push button?
Some hints on how you can get started with your problems:
You could create an axes object in your figure, whose position can be defined. then just plot the image on that axes. Do all that in the callback
Calling a function from a callback should not be a problem
Save the image in structure, then you can use for example setappdata and getappdata to pass it between callbacks, i.e. when your figure handle is h.fig and your structure called d:
setappdata(h.fig,'d',d)
in the first callback, and to retrieve it, in the second:
d = getappdata(h.fig,'d');

Matlab rotate3D and buttondownfcn incompatibility

Hi everyone and thanks in advance for all your help.
I'm currently working on a UI in matlab that includes 4 differents axes to show various info. Basicly, I have MRI data (so 3D data) that I want to show. I use 1 axe to show 3 perpendicular plane that split the data at its center (axeMain), and 3 other axes that shows the planes individually (axeX,axeY and axeZ). I have a main axe on wich I want to make rotations possible (only on the main axis system) and I want to be able to select coordinate on the 3 other axe (those with 1 plane only). I also unable translations but that is out of scope for my problem.
So basicly, I have no problem selecting my coordinates (using the buttondownfcn on my planes) on all of the 3 axes and I also have no problem using rotate3D on the mainAxe. HOWEVER, if I try to have both to work at the same time, the buttondownfcn doesn't work anymore. I don't understand why it's doing this (I have some ideas but that's about it) and I have no idea how to work around it. Basicly my code for those functions are like this :
rotate3d(handles.axisMain);
%some other code, setting up other UI stuff
%allow selection on the 3 static plains.
set([handles.axeX,handles.axeY,handles.axeZ], 'buttondownfcn', #getCoord);
So my question is basicly : why wont those 2 functions don't work together and how to work around that problem, ideally with minimal code change?
Thanks.
EDIT : this is a print screen of my current interface. I want to be able to select coordinate using ONLY on the 3 last axes (containing only 1 plane in each of them). I also want to be able to rotate images ONLY on the first axe (the one with 3 planes). Hope this clarify.
I would also like to note this : following my tests, I found that the mouse click would not raise at all if the rotate3D is activated. The problem is not in the logic I use to get the coordinates itself, but in the click event not being fired.
Also, I am aware that the event is not on the surface that I try to print. The actual code is like this :
set(h, 'buttondownfcn', #getCoord);
set(h,'HitTest','on');
where h is the handle of a surface and each surfaces are processed this way.
I have found the exact solution!
Exploring the code for rotate3d(hAxe,'on') , i have found that they key line is getuimode(hFig,'Exploration.Rotate3d') wich returns uitools.uimode that contains ButtonDownFilter and ModeStateData methods.
Finally i have solved the problem in the following way:
rotate3d(hAxe,'on')
uiMode = getuimode(hFig,'Exploration.Rotate3d');
uiMode.ButtonDownFilter = #mycallback;
where:
hAxe is the handle to the axes object
hFig is the handle to the figure object
#mycallback is the callback that lets buttondownfcn and rotate3d works together as in the example of Matlab's help
So, you have to tag the object that you want to not be ignored:
set(hAxe,'tag','DoNotIgnore')
And write the callback:
function [flag] = mycallback(obj,event_obj)
% If the tag of the object is 'DoNotIgnore', then return true
objTag = obj.Tag;
if strcmpi(objTag,'DoNotIgnore')
flag = true;
else
flag = false;
end
maybe
set(handles.axisMain, 'buttondownfcn', #getCoord);

Adding text annotation to figure programmatically

I would like to add text annotation to a figure from a GUI made with GUIDE. First I plot some data, than when the user clicks on a checkbox I call the text function in the event handler like this:
text(obj.XData(q), obj.YData(q)+10, int2str(q), 'Units', 'pixels');
obj is the line object itself, q is a counter for each point in the plot. It runs without errors, but nothing happens. I suppose I should 'refresh' the axis somehow, but refresh command doesn't help and I haven't found anything in the documentation.
Edit: I have found out that my code was wrong: pixel units correspond to a coordinate system where the origin is the lower-left corner of my axis control, what is not the same as my data coordinate system. I fixed this problem with ds2nfu, and when I paint before plotting everything is fine. But after plotting I see nothing. Is it possible that there is some kind of z-order problem with annotations?
As I mentioned in the comments, you should use the normalized units to place things in the same coordinate system as the data.
Now for the other problem. I'm not sure if this is a z-order issue, but if it is, you can bring the text to the front using UISTACK:
hText = text(x,y,'str');
uistack(hText, 'top')

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).