How to modify the visibility of different layer in a figure plotted by Matlab when I finished the plotting? - matlab

A figure file is saved. When several lines are intersected, I want to make one of the line visible. How should I modify the different layer of the lines without re-plotting the figure?

Use uistack (see doc). For example, after:
figure
hold on
hblue=plot([1 2],[3 4],'b','LineWidth',5);
hred=plot([1 2],[4 3],'r','LineWidth',5);
the red line is on top (and the blue line would not be seen if the the red line covered it). Then, if you use uistack(hblue,'top'), the blue line is brought to the top. Other options to reorder plots, instead of top, are up, down, and bottom. You can optionally specify the number of steps up or down (e.g. uistack(h,'up',2) to move a handle two layers up - though no need in my simple example).
If, as you say, the 'figure file is saved', and you don't have the handles for the plots, (hblue and hred in my example), after loading the plot, you can get the handles using get(gca,'children').

If I understood you correctly, try to use hold on before plotting...

Related

MATLAB R2014b: Rendering plots with lines in the same place

Since version R2014b, MATLAB now renders graphics nicely anti-aliased (finally!)
However, this causes a glitch in the way it displays some of my figures. If I plot a line, use hold on and then plot another line in exactly the same place with a different colour, the line appears in a mottled combination of both colours. In the past, the line would simply appear as the last colour that was plotted in that location.
Here's an example of a trace in blue, with some sections (the steeper bits) showing a green line. In previous MATLAB versions, the green lines would be continuous, but now some of the blue line shows through.
Is there a neat way to work around this in the new version, or do I have to ensure that I remove any existing lines before plotting in the same place?
When overplotting, the new anti aliasing plots can have some bleed through, try
set(gcf,'GraphicsSmoothing','off')
To see if it restores the functionality you're used to.
Ref:
http://www.mathworks.com/matlabcentral/answers/157758-how-do-i-turn-off-antialiasing-in-matlab-r2014b

How to prevent the fill command in MATLAB from creating boxes without "corners"

I am currently using the fill command in MATLAB to create a graph of boxes that were created using the 'fill' command (the actual code is based off this StackOverflow Question.
My issue is that the boxes that I create do not have "corners." I am attaching a PNG that illustrates the issue. Note that you have to look a little carefully since the image was heavily rendered, though in this example my arrows also look weird since they don't have edges either)
I am wondering if anyone has an idea of what might be going wrong? The boxes appear this way immediately after I use the fill command, which has the following structure:
fill(X,Y,MyFaceColor,'FaceAlpha',0.5,'EdgeColor', MyEdgeColor,'LineStyle','','LineWidth',box_line_width,'EdgeAlpha',1)
The function fill appears to leave space for corner markers if they are not explicitly defined. Hence, calling fill with the marker property will solve your problem. However, since markers and linewidths seem to work on different scales, you will have to play around with the marker size to get smooth edges.
Example:
fill(X,Y,'r','FaceAlpha',0.5,'EdgeColor', 'k',...
'LineWidth', 5,'EdgeAlpha',1 , 'marker', '.', 'markersize', 15)

MatLab: displaying figures side by side

As when you use subplot to display plots side by side in the same window, I would like a similar function that can do the same for multiple figures. This works:
subplot(2,2,1)
and I am looking for something like this:
subfigure(2,2,1)
Does a build-in function with this advantage exist?
You could write one such function yourself. The idea is to get the screen size:
get(0,'ScreenSize')
then divide it up into sub-regions according to the input parameters. You would also need to account for margins in-between.
Another idea:
create an invisible figure (preferably same aspect ratio as the screen)
call subplot inside it
capture the position of the created axis
delete figure
scale the position captured to fit the screen size, and use it to create the actual figures.

Matlab: opening propertyeditor or plotbrowser forces correct textbox annotation; annotation fails without it

I have a generally happily running program that takes files, plots them, spits out a pdf (letter size). I use annotations to put in a title above a set of three subplots, and to use as a footer with file info and date. I would like the title to be at the top of the page, filling up from margin to margin, centered.
I have two ways of running the program: in 'batch' mode and 'interactive' mode. When in 'interactive' mode, I create the figure with a simple figure() command. When in 'batch' mode, I create the figure with figure('visible','off'). Here is my command for making the annotation:
annotation(obj.hFigure(f),'textbox',[0 0.9 1 0.1],...
'String',title,...
'HorizontalAlignment','center',...
'FontSize',18,...
'LineStyle','none',...
'FitBoxToText','off');
Here, "obj.hFigure(f)" is simply a handle to the figure I am currently processing. As you can see, I place the figure near the top of the figure, and make sure that the text runs off the bottom of the box (in case it is larger).
My problem is with margins on the above annotation. In batch mode (no figures showing), I get 10% or so margins on either side of the text, which ruins the layout. In interactive mode (figures show up), I don't get the margins: the text correctly flows from one edge to the other.
I have narrowed down the problem to the following: I can get the correct response to the ps printing in batch mode if I make the figures visible (figure('visible','on')) AND open up
propertyeditor(gcf);
plotbrowser(gcf);
after each figure is plotted. This makes the program take about twice as long (which isn't a huge deal). But what I don't understand is: what do those two commands do that drawnow or refresh don't accomplish?!
I am unsure about your specific case, but when encountering this kind of problem in the past I have had great success by explicitly setting the figure size with:
set(gcf, 'Position', [100 100 300 300])
and then, before printing/saving setting the PaperPositionMode to auto, which seems to force the printed figure to be the same size as the one shown on screen:
set(gcf, 'PaperPositionMode','auto')

Strange behavior in Matlab when exporting figure to eps, pdf

When I make a figure in Matlab, with a legend and a rectangle that touches the y axis (strange, I know) upon exporting the figure to eps (or pdf) I've noticed that the rectangle obtains the line-style of the last line drawn (rather than what the rectangle was drawn with)
This behaviour also occurs for rectangles drawn after the one that touches the axis...
This doesn't happen if the rectangle is drawn before the legend is created....
Needless to say, it took me half a day to create a minimal example:
clf
L=plot(X,sin(X),'--');
legend(L,'sin(x)')
rectangle('position',[0.001,.1,.7,.7])
rectangle('position',[0,.5,.6,.7])
rectangle('position',[0.001,.3,.5,.7])
%legend(L,'sin(x)')
On the screen the 3 rectangle have solid lines, as they should. but once they are exported, the result has the last two with dashed lines (like the sin(x)). If the legend command is done later (as in the commented out line), everything works as it should....
Is this a feature or a bug?
This is not a feature. I am submitting this to development.
You found a workaround that works with minimal code gymnastics. I would document it in your code so someone does not change it unknowingly and move on.
If you are open to other output formats, notice this is not an issue with formats that use an output filter of MATLAB.
http://www.mathworks.com/access/helpdesk/help/techdoc/ref/print.html
(Graphic Format Files section, right column in table)
-Doug, Advanced Support at MathWorks dealing with graphical issues.