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!
Related
I have a simple block.
I create a mask using this block.
Then I edit the mask using popup. Now i have 2 choices. Sin or Cos.
This is where it gets complicated. I want my block which I called as 'Result' in the constant block is equal to 1 if i chose sin and , 0 if I chose answer as cos. I tried to write a callback function but I couldnt do it.
I am kinda new to these staff. Hope you can help me.
Thanks.
There are at least 3 problems:
You do not want your variable to be evaluated, so you need to de-select that option in the Mask Editor.
You do not want to call your variable Result. At present your mask has Result being a string (either sin or cos), but the model expects Result to be a numeric value. Let's assume you change the mask to create a variable called fcn_selected instead.
You have not shown us your callback code, nor told us where you put the code.
For 3. you want to write code something like
switch fcn_selected
case 'Sin'
Result = 1;
case 'Cos'
Result = 0;
end
And you want to put the code on the Mask Editor's Initialization pane.
I read the blog post on assigning transparency to plot markers. I tried the code on a simple example and all was well. Then I tried a tight loop, plotting a single point at a time (doing this to assign a different color to each point in the graph), and invariably within a few loop cycles, when I grab the "plothandle.MarkerHandle", it's empty. In these cases, the class of this empty object is Matlab.graphics.GraphicsPlaceholder
while when the operation is successful, the class is:
matlab.graphics.primitive.world.Marker
The basic loop follows. colormatrix assigns a [r,g,b] color to each data point.
hold on
opacity = 0.5;
for jk = 1:numel(idx
tmph = plot(foox(jk),fooy(jk),'o','color',colormatrix(jk,:) );
tmpk = tmph.MarkerHandle;
tmpk.FaceColorData = uint8(double(tmpk.EdgeColorData).* [1,1,1,opacity]');
tmpk.EdgeColorData = uint8(double(tmpk.EdgeColorData).* [1,1,1,opacity]');
end
I've tried things like clearing variables every loop, putting in a delay timer, and so on, with no luck. I'm using Matlab R2015a.
EDIT: here's a simple example. What I seem to be finding is that if I run the entire script, it always fails. If I break it into two pieces where noted and execute the second section with a separate key stroke (ctrl-enter or selectall/F9 in the IDE editor), everything works. And yes, I'm aware that "undocumented features" are risky , but since MathWorks still hasn't figured out that allowing transparency -- and indexed color assignments -- are good things for the plot function, I'm still looking for a better workaround than using patch to draw each data point.
figure
xfoo = 1:10;
yfoo = 2*xfoo;
tmph = plot(xfoo,yfoo,'p','color',[1,0,1]);
hold on
opacity = 0.7;
% wait a while here.
tmpk = tmph.MarkerHandle;
tmpk.FaceColorData = uint8(double(tmpk.EdgeColorData).*[1,1,1,opacity]');
tmpk.EdgeColorData = uint8(double(tmpk.EdgeColorData).*[1,1,1,opacity]');
The fact that the script seems to work if you wait a bit between the plot and the retrieval of tmph.MarkerHandle suggests that you have the same issue that was reported on the blog by a user running R2014b. Yair suggested to call drawnow after the plot:
figure
xfoo = 1:10;
yfoo = 2*xfoo;
tmph = plot(xfoo,yfoo,'p','color',[1,0,1]);
hold on
opacity = 0.7;
drawnow;
tmpk = tmph.MarkerHandle;
tmpk.FaceColorData = uint8(double(tmpk.EdgeColorData).*[1,1,1,opacity]');
tmpk.EdgeColorData = uint8(double(tmpk.EdgeColorData).*[1,1,1,opacity]');
The workaround didn't work for a user running R2015a, which doesn't sound promising, but the fact that waiting seems to help for you, is encouraging.
I made a GUI where i would like to have the position from, in pixels. I set the "Resize behaviour" to "Proportional"
Now i use
Position = getpixelposition(Test)
Test is the name of the GUI. I added a button to the Gui. Once i click on the button the code runs. But what happens, is that the screen shifts location. It was full size, now it shifts. 38 pixels to the right and 61 down.
Anybody an idea why this is?
Maybe other solutions to get the postition of my GUI?
I can't reproduce this behavior on my system (Win32 + MATLAB2012a).
However, to see what causes it:
type in your Command Window:
edit getpixelposition
set a breakpoint at the beginning of the function;
trigger the code tha calls getpixelposition;
step into the function (F11 preferably), and notice when the change of the position takes place; the executed statement would be the cause.
To get the position in pixels, you may use the undocumented hgconvertunits. From the .m file:
Y = HGCONVERTUNITS(FIG, X, SRCUNITS, DESTUNITS, REF) converts
rectangle X in figure FIG from units SRCUNITS to DESTUNITS
using the object with handle REF as the reference container for
normalized units. REF can be the root object.
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.
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);