What's the field axes in the last line of the following code?
function drawBox2D(h,object)
% draw regular objects
if ~strcmp(object.type,'DontCare')
rectangle('Position',pos,'EdgeColor',occ_col{object.occlusion+1},...
'LineWidth',3,'LineStyle',trun_style{trc},'parent',h(1).axes)
rectangle('Position',pos,'EdgeColor','b', 'parent', h(1).axes)
See the h(1).axes in the last line? I don't know what is the h, don't know what to pass to that function.
I speculate it to be a graph handle. So I tried
h = figure(2);
drawBox2D(h, obj);
However the handle itself is an integer so there's no field called 'axes'.
Could anyone tell me who does the field axes belong to? And what should I pass as h into this function?
If you want to get all axes handles anywhere in Matlab, you can do the following things:
allAxes = findall(0,'type','axes');
To select only axes handles not legends, you need to cleanup the list of axes
axNoLegendsOrColorbars= ax(~ismember(get(ax,'Tag'),{'legend','Colobar'}));
Related
I have figures (in Matlab's fig file format) each of which contains a line plot with two lines (representing EEG curves), axes, bunch of labels etc.
I want to:
change the color of the lines
remove some of the labels
I would loop over the fig files and do the same thing for each of them.
Is there a list of all the objects within the figure that I could index and edit? How can I get to those objects using commands (i.e. without the gui)?
Line, lable, etc. are children of axis, which is itself a child of a figure. What you need to do is acquire handles to the objects you want to change through this hierarchy.
% Get a handle to the figure
hfig = openfig('testfig');
% Get all children of the CurrentAxes. Most of what you want is here.
axes_obj = allchild(hfig.CurrentAxes);
% Edit Axes object according to its type
For ii = 1:length(axes_obj)
switch axes_obj(ii).Type
case 'Text'
% Do something, for example:
axes_obj(ii).String = 'changed';
case 'Line'
% Do something, for example:
axes_obj(ii).MarkerEdgeColor = 'b';
end
end
% Save figure
savefig(hfig, 'testfig')
You can see all the properties of the object you wish to edit by simply typing axes_obj(ii) in the command window.
I need a help with MatLab GUI .
I have a GUI with an axes on it,
and a function plotData(axes,data) which has axes as parameter.
The GUI has a button "plot Data".
How can I do the following:
When the button is clicked, call the function plotData with the parameter axes1 and the data that I want to plot?
I want the plot to be directed to axes1 which exists in the GUI.
It's suppose to be simple, but when I send the axes as parameter it doesn't plot on the GUI, or maybe it does but I cant see it.
It works fine for me without the function: just to plot the data. but to plot the data it's not 1 row :).
i tried calling ax which storing the GUI's axes handle on different M file, but since i call it as a function from different M file nothing happens with the GUI axes handle but it also not returning any error.
Side remark: Your question is a bit unclear: if you added small code snippet to illustrate what you have tried, better answers could be provided.
To the question at hand:
Have you tried directing the plot to axis1 in plotData?
function [] = plotData( ax, data )
% make ax the current axes for plot
axes( ax );
% continue with plotting the data
% ...
You can achieve the effect of axes( ax ); in a more efficient way through the specific plot commands you are using.
For example, if you are using simple plot
plot( ax, data ); % plots data to axes ax
check the documentation of the specific plot command you are using for the axes argument.
If f is the figure handle, I wanted to use plot3(..) on it just like I would use plot(..), but this didn't work:
>> plot3(f, t, real(Y), imag(Y))
Error using plot3
Vectors must be the same lengths.
Then I figured out that the way to do this is to:
First make the relevant figure current.
Then use the plot3(..) function.
I can find what the current figure is using gcf, but how do I make a figure current (via its handle)?
This method has my personal preference:
set(0, 'currentfigure', f); %# for figures
set(f, 'currentaxes', axs); %# for axes with handle axs on figure f
because these commands are their own documentation. I find
figure(f)
and the like confusing on first read -- do you create a new figure? or merely make an existing one active? -> more reading of the context is required.
It is actually as simple as feeding the f back into the figure(..) command:
figure(f) %Makes the figure current.
Also, if I did something like this:
f = figure('IntegerHandle','off'); % With unique, non-reusable handle.
top = subplot(2, 1, 1);
bot = subplot(2, 1, 2);
Then I can make the axes top or bottom current by issuing a command like this:
subplot(top);
This also works:
axes(top);
But the two types of handles cannot be intermixed: axes(..) and subplot(..) work on axes handles, while figure(..) works on figure handles.
While others have provided you exactly what you've asked for (how to make an axes or figure the current one). My preferred way for dealing with this, is to explicitly specify the parent of your plot in the call to plot3.
If you look at the documentation, you will see that you can specify the parent axes as the first parameter to the function. If looks like you attempted to do this in your example, but you provided a handle to a figure rather than an axes.
f = figure()
ax = axes('Parent', f)
im = plot3(ax, X, Y, Z);
Alternately, I prefer the explicit solution
im = plot3(X, Y, Z, 'Parent', ax)
The nice thing about this explicit parameter/value specification of the parent is that it is accepted by all graphics objects. Functions like plot and plot3 are actually helper functions that wrap the functionality of line and allow for the convention of passing the parent first. The parameter/value approach is widely accepted regardless of whether you're working with a higher level function (plot, plot3, imshow) or the lower level objects (line, image, etc.)
The two benefits here are that you remove the overhead of MATLAB trying to figure out where to put your plot and also, it prevents MATLAB from having to change which figure is currently displayed, forcing a re-rendering which is one of MATLAB's slowest tasks.
give handle name to figure, give you a little example
f1 = figure;
imshow(image1);
f2 = figure;
imshow(image2);
% edit image 1
figure(f1);
text(2,3,'done');
I have a function, myFunkyFigure, that takes in data, does some funky things, and returns an axis object for the figure it produces.
I would like to be able to invoke this function twice, creating two different figures:
fig(1) = myFunkyFigure(dataSet1);
fig(2) = myFunkyFigure(dataSet2);
Then I would like to put them into a subplot together.
Note that, because of the funkiness of myFunkyFigure, the following does not work.
subplot(2,1,1);
myFunkyFigure(dataSet1);
subplot(2,1,2);
myFunkyFigure(dataSet2);
I believe that I need something along the lines of copyobj, but I haven't been able to get that to work (I tried following a solution in Stack Overflow question Producing subplots and then combine them into a figure later in MATLAB but to no avail).
Obviously, we don't know how "funky" your figures are, but it should be noted in such a case that the cleanest solution would be to modify the function myFunkyFigure such that it accepts additional optional arguments, specifically the handle of an axes in which to place the plot it creates. Then you would use it like so:
hSub1 = subplot(2,1,1); %# Create a subplot
myFunkyFigure(dataSet1,hSub1); %# Add a funky plot to the subplot axes
hSub2 = subplot(2,1,2); %# Create a second subplot
myFunkyFigure(dataSet2,hSub2); %# Add a funky plot to the second subplot axes
The default behavior of myFunkyFigure (i.e. no additional arguments specified) would be to create its own figure and place the plot there.
However, to answer the question you asked, here's a way to accomplish this given that you are outputting the axes handles (not the figure handles) in the vector fig (note: this is basically the same solution as the one given in the other question, but since you mention having trouble adapting it I thought I'd reformat it to better fit your specific situation):
hFigure = figure(); %# Create a new figure
hTemp = subplot(2,1,1,'Parent',hFigure); %# Create a temporary subplot
newPos = get(hTemp,'Position'); %# Get its position
delete(hTemp); %# Delete the subplot
set(fig(1),'Parent',hFigure,'Position',newPos); %# Move axes to the new figure
%# and modify its position
hTemp = subplot(2,1,2,'Parent',hFigure); %# Make a new temporary subplot
%# ...repeat the above for fig(2)
The above will actually move the axes from the old figure to the new figure. If you want the axes object to appear in both figures, you can instead use the function COPYOBJ like so:
hNew = copyobj(fig(1),hFigure); %# Copy fig(1) to hFigure, making a new handle
set(hNew,'Position',newPos); %# Modify its position
Also note that SUBPLOT is only used here to generate a position for the tiling of the axes. You could avoid the need to create and then delete subplots by specifying the positions yourself.
The code from gnovice didn't work for me.
It seemed that a figure couldn't be made the child of another figure. For example hNew = copyobj(fig(1),hFigure); gave the error
Error using copyobj
Object figure[1] can not be a child of parent
figure[1]
Instead, I had to make the axes children of the new figure. This is the function I came up with
function []= move_to_subplots(ax,a,b)
% %
% Inputs:
% inputname:
% Outputs:
% name: description type units
% saved data: (does this overwrite a statically named file?)
% plots:
%
% Standard call:
%
%
% Written by C. Hogg Date 2012_06_01
%
%
debugmode=0;
hFigure=figure();
if ~exist('a')
a=ceil(sqrt(length(ax)));
end
if ~exist('b')
b=1;
end
if a*b<length(ax)|~exist('a')|~exist('b')
disp('Auto subplot sizing')
b=ceil(length(ax)/a);
end
for i=1:length(ax)
hTemp = subplot(a,b,i,'Parent',hFigure); %# Make a new temporary subplot
newPos = get(hTemp,'Position'); %# Get its position
delete(hTemp);
hNew = copyobj(ax(i),hFigure);
set(hNew,'Position',newPos)
end
%% Debug. Break point here.
if debugmode==1; dbstop tic; tic; dbclear all;end
end
This seems to work for me.
How do I obtain all the axes handles in a figure handle?
Given the figure handle hf, I found that get(hf, 'children') may return the handles of all axes. However, the Matlab Help suggests that it may return more than just the axes handles:
Children of the figure. A vector containing the handles of all axes, user-interface objects displayed within the figure. You can change the order of the handles and thereby change the stacking of the objects on the display.
Is there any way to obtain only the axes handle in the figure handle? Or how do I know if the handle returned by get(hf, 'children') is an axe handle?
Thanks!
Use FINDALL:
allAxesInFigure = findall(figureHandle,'type','axes');
If you want to get all axes handles anywhere in Matlab, you could do the following:
allAxes = findall(0,'type','axes');
EDIT
To answer the second part of your question: You can test for whether a list of handles are axes by getting the handles type property:
isAxes = strcmp('axes',get(listOfHandles,'type'));
isAxes will be true for every handle that is of type axes.
EDIT2
To select only axes handles that are not legends, you need to cleanup the list of axes (ax handles by removing all handles whose tag is not 'legend' or 'Colorbar':
axNoLegendsOrColorbars= ax(~ismember(get(ax,'Tag'),{'legend','Colobar'}))
Jonas' solution didn't work for me, because there were some handles referring to legends. Surprisingly, legends seem to be implemented as axes, at least in Matlab 2010a.
Here is a solution if you only want the axes, not any legends or other stuff.
axesHandles = get(fig, 'Children');
classHandles = handle(axesHandles);
count = length(axesHandles);
isNotInstanceOfSubtype = false(1, count);
for i = 1:count
isNotInstanceOfSubtype(i) = strcmp(class(classHandles(i)), 'axes') == 1;
end
axesHandles = axesHandles(isNotInstanceOfSubtype);
The script works by sorting out handles which reveal to be of a sub-type of type axes, for example scribe.legend.
A warning for those trying to improve the above code snippet: using something like
classHandles = cellfun(#(x) handle(x), axesHandles)
might not work as intended:
??? Error using ==> cellfun
scribe.legend type is not currently implemented.
"Jonas" and "tm1" have answers that work for some. However, as tm1 pointed the issue, there are several items inside type 'axes'.
To exactly refer to the legend or axes itself (there may exist other items), you need to differentiate them, using their characteristic properties.
In my example, I opened "property editor" and looked for differing properties for axes and legend (since they both belong to "type, axes"). I was trying to copy my axes and its legend:
copied_axes = findobj(temp_fig,'type','axes','Tag','');
copied_legend = findobj(temp_fig,'type','axes','Tag','legend');
Instead of 'Tag' property, I also could use an other property from the "Property Inspector". The thing is, they must differ. Most of their properties are the same.
The solution by #tm1 is excellent. Mine is a little less complicated (if you're ok with functional programming):
% initialize `fig` somehow, i.e., "fig=gcf()" for the current figure or
% "fig=get(0,'children')" for all open figures; can be vector or scalar.
ax = findall(fig, 'type', 'axes');
ax = ax(arrayfun(#(i) strcmp(class(handle(i)), 'axes'), ax));
ax will contain only plot axes. This works because the class of a legend or colorbar object is different from axes.
Edit #Jonas points out a potential improvement to filter the results of findall, because at least legends and colorbars seem to have non-empty Tag properties: replace the last line in the above code snippet with
ax = ax(strcmp('', get(ax, 'Tag')))
Both these techniques are kludgy and may break in the future (a comparison against ggplot2 or Bokeh might be interesting).