MATLAB: difference between getpts(ax) getpts(fig)? - matlab

I was looking at this question, and the answer by Marc, and I can't get the getpts function to work as described. (BTW, I wanted to just comment on Marc's answer, but the site doesn't let me comment until a rep of 50.) According to documentation, and Marc, using
[x,y] = getpts(ax);
is supposed to restrict the user's point selection to within the axis, and prohibit point selection in the rest of the figure. Right??
Well, if so, it's not working. I'm writing a program in which I want the user to specify a part of the image to chop off. I made a figure with an image via the imagesc() command. I then try to do the following:
set(0,'CurrentFigure',1) (Because he program has more than one figure open)
ax = gca;
[x,y] = getpts(ax);
The small cross cursor comes up, and I can click and select points anywhere in the figure, including in the gray border area outside of the image. This is exactly the same behavior as I get if I run [x,y] = getpts(gcf); So, what's going on here?? Either way, the point selection is allowed in the entire figure window and NOT restricted to just the axis area where my image is...
My Matlab version is R2014a.

Related

Matlab second set of axes not the same dimensions as the first

What I really want to do is what this question is all about:
matlab remove only top and right ticks with leaving box on
To paraphrase the original question, I want to have a plot with tick marks on the left and bottom axes but NO ticks on the top and right axes, but with the box outlining the plot still intact.
Since I don't have enough reputation, I wasn't able to simply comment to follow up. Anyway, I execute the same code given in the answer by mc2, and what I get is the figure below. The logic of the method is to use 2 axes that have the same dimensions and overlay them on top of each other. The first axis has the 'box' property turned off, the second one does not. The code is:
a = gca;
set(a,'box','off','color','none')
b = axes('Position',get(a,'Position'),'box','on','xtick',[],'ytick',[]);
axes(a)
Assume that the axis that represents "a" already exists. That's the initial figure. (I added the blacked out part on top of the screen shot.) The second axis that was created is clearly not the same dimensions as the initial one--it's the bigger, nearly-square box that surrounds the smaller, rectangular one. Instead, I want the big, nearly-square box to be same dimensions as the smaller one.
Somehow the get(a,'Position') function is not grabbing the correct dimensions that I want it to. Why is this happening?
I didn't make the original figure myself; a collaborator generated the data and sent me *.fig file.

Matlab: How to change the linewidth in a figure before actually plotting some?

This question maybe a bit like the link below, but this didn't work for me... http://nl.mathworks.com/matlabcentral/answers/102530-how-can-i-change-the-default-settings-for-the-linewidth-property-before-i-plot-a-figure-in-matlab
I'm working on a matlab function that automatically opens your figure in full screen mode and on a second monitor if present. So far, everything works fine. I already achieved to set the fontsize inside the function, so whitout plotting anything and without making xlabel(..) etc.:
% Fontsize used at the figure
if ~exist('fontsize_manual','var')|| isempty(fontsize_manual)
set(gca,'FontSize',16)
else
set(gca,'Fontsize',fontsize_manual)
end
Now is my question: Can I change in a same way the linewidth of the lines that will by plotted in the figure? So also here, predefining the linewidth inside the function and later on in your script plotting some lines etc. I do prefer this works only for the figure you're working on, so that you can change this 'default' for each figure and save them all with different linewidth and fontsizes if needed.
I tried the line below, but that only changed the linewidth of the axis.
set(gca,'LineWidth',2)
Is there anyone who can help me solving this problem?
%------------------------------------------------------------------------------------------------------------------------------
The answer below is nice, but I detected a new problem.
The code below in found accidentally by solving the previous problem:
set(gca,'LineWidth',3)
It turned out this changes the width of the axes. But now the problem... Also here this works only on the first figure. (see figure)
If I also put this code in my session after the plotting in the second figure, the width in the second figure changes. Looks like the right handle isn't reached, or something like that, inside the function, when making the second figure. Do you have any idea what could be wrong here?
I think what you are after is the DefaultLineLineWidth property, to which you can assign a value for a particular figure (or the root).
Here is a simple code illustrating; basically I create a figure, set its 'visible' property to 'off' and assign a default line linewidth (that sounds weird...). The line plotted has a linewidth of 4, whereas another plot created after has the default width:
clear
clc
hFig1 = figure('Visible','off'); %// Create figure, set it to not visible.
set(gcf,'DefaultLineLineWidth',4); %// Assign default linewidth.
x = 1:10;
plot(x,x.^2-5);
set(hFig1,'Visible','on')
title('Figure 1','FontSize',16);
hFig2 = figure;
plot(x,2.*x+rand(1,10));
title('Figure 2','FontSize',16);
Plots:
Hope that help!
The link shown sets the property of the root (and so all figures should inherit). (This worked for me)
set(0,'defaultlinelinewidth',2)
You can also try a similar set command like the one you suggested, but change it to this:
set(gcf,'defaultlinelinewidth',2)

How to merge two figure files into a single file

This should be a problem with a trivial solution, but still I wasn't able to find one.
Say that I have 2 matlab figures fig1.fig, fig2.fig which I want to load and show in the same plotting window.
What should I do?
I mean, I am pretty sure that I can accomplish the task using some low(er) level graphic command which extracts contents from one image and put them in the second one, nonetheless I cannot believe that there is not any high level function (load fig2 on top of fig1) that does this...Comparing 2 plots (unfortunately already saved) is a very common task, I'd say.
Its not clear if you want to extract data from the figures and compare the data, or if you want to combine the plots from two figures into a single figure.
Here is how you combine two figures into one (if thats what you want to do)..
First load the figures:
fig1 = open('FigureFile1.fig');
fig2 = open('FigureFile2.fig');
Get the axes objects from the figures
ax1 = get(fig1, 'Children');
ax2 = get(fig2, 'Children');
Now copy the hangle graphics objects from ax2 to ax1. The loop isn't neccesary if your figures only have a single axes
for i = 1 : numel(ax2)
ax2Children = get(ax2(i),'Children');
copyobj(ax2Children, ax1(i));
end
Note This example assumes that your figures have the same nubmer of axes and that you want to copy objects from the first axes in the second figure to the first axes on the first figure. Its up to you to figure out the proper indexing if the axes indices aren't lined up.
The answer slayton gave is good. Here's another tip: If you have two plots opened in two separate Matlab figure windows, don't forget you can point-and-click copy the proper plots. Do this by clicking the arrow pointer in the Matlab figure window, and then clicking on the plotted line. Copy the (plotted line, textbox, etc...) object. Then, similarly select the axis in the other Matlab figure window and paste it.
I give this 'silly' solution because it has proven to be useful in in collaboration meetings. Point-and-click copying in front of someone (like your adviser) communicates exactly what curves are being compared, and it prevents you from having to fire up code in front of others.
You can also go to File in the menu, Generate Code, for each plots.
Then copy and paste both in the same mfile, with a "hold on" in between and changing details related to the appearance.
Then run the new m-file.

how to always display "labels" on axis X

If I zoom several time graph all labels from axis X disapear (go away) and there are no visible axis X labels so it is not possible to understand the part of graph where am I.
How can I force matlab to always display labels on axis X and to update them automatically while zooming and to display enough digits so "neighboor" labels must be different.
it depends, are you manually setting the tick marks yourself ('XTick' and 'XTickLabel' axis properties)?
Try this simple example
plot(sin(1:10), 'o-')
without changing anything, you can zoom as much as you want, and the tick labels will always be visible
EDIT
The root cause of the problem is the same as the one raised in your other question, datetick function will manually set the tick labels, thus disabling automatic update on zoom/pan.
The good news is there are already submissions on FEX that tries to solve this exact problem with DATETICK
I run into the same problem even on the new version of MATLAB (r2014). MATLAB does not display sufficient x-axis tick labels as you zoom-in. After several experiments I found the following workaround. Following is a plot before implementing the solution. MATLAB displays only three XTick labels on the x-axis even though there is sufficient space for more (there are often even less labels as you zoom in more).
Suspecting that MATLAB thinks that it does not have sufficient space to display more labels, a workaround can be to rotate the labels. To do that, after you issue the plot commands, e.g.
plot(tsX);
hold on;
plot(tsY);
plot(tsZ);
add the following command
set(gca,'XTickLabelRotation',90);
Now MATLAB plots with more labels
I am going to report this as a bug to the MATLAB guys.

How do I hide axes and ticks in matlab without hiding everything else

I draw images to axes in my matlab UI, but I don't want the axes and ticks to be visible how do I prevent that, and also where do I make this call?
I do this
imagesc(myImage,'parent',handles.axesInGuide);
axis off;
Is this what you are looking for?
This is definitely somewhere else on this website and in the matlab documentation. Try typing
help plot
Or using the documentation on plotting!
edit: Now that you have shown what you are doing. (You don't need the handles, I just always write them in to clutter my workspace)
myImage = yurbuds0x2Dironman; # don't ask
fH = figure;
iH = imagesc(myImage);
set(gca,'xtick',[],'ytick',[])
Are you able to do it like this?
I support the
set(gca,'xtick',[],'ytick',[]);
approach over the
axis off
one. The reason is set(gca, ...) just removes the labels but keeps the axes, unlike axis off. I am generating a group of images with fixed dimensions to combine later into a video. Deleting the axes creates different size frames that can't be recombined.