Activate a specific figure when returning from a nested function - matlab

I have a plotting function inside imshow() command.
The plot and the image should be in separate figures.
At the return from the inside function the current figure is of the plot thus imshow() puts the image onto the same figure of the plot and kills the plot.
What can be done to make imshow() open or get to an existing its own figure, while keeping such manner of nested function calling?

Well, apparently, I've found an answer by the time I was done with the question, but because I find it interesting enough and find nothing answering it I'm writing the answer as well.
The algo is like this:
open a figure before imshow(nested_function());
upon starting the nested_function() save the handle to the
previous figure like fh_prev = gcf; for example
do any plots along the nested_function()
before return from the nested_function() activate the previous
figure with the command figure(fh_prev);

Related

Matlab skipping plots (uncertainty)

I have a sequence of finding fits for data (cd to folder, read and fit the data) and subsequently plotting them in a loop.
I observe that few plots are done incorrectly, skipped and added in subsequent plots. A four second pause between each plots seems to solve this issue.
I assume everything is sequential in Matlab, meaning subsequent commands wait until current command is finished. I believe this is not the way happening now. I believe using pause is not the best solution. Can someone provide a fix for this?
A set of subsequent plots without the pause (solid line is a fit of datapoints on the fly):
The same plots with pause of 4 seconds:
subplot, and most other functions that generate graphics objects, provide a handle to the generated graphics object that you can use to address the object explicitly with functions like plot.
If an explicit axis handle is not provided to a plotting function, it will use the current axes, which can very often lead to issues like these. So much so that it's caveated in the documentation:
User interaction can change the current axes or chart. It is better to assign the axes or chart to a variable when you create it instead of relying on gca.
So rather than doing:
axes
plot(1:10)
You should do the following:
ax = axes;
plot(ax, 1:10)

Drawnow on Matlab: It store the figure or store the variable?

May you please help me on a question on DRAWNOW in Matlab?
When we using drawnow in Matlab, what happens inside?
It stores the figure of the previous-graph, then plot the next-part-of-graph on the same figure?
OR it forgets the whole previous-graph and plot the actual-new-graph (with both previous and next-part)?
The both methods show the same effect: a dynamic graph. But I want to know exactly what happens inside.
Thank you!
drawnow makes sure that MATLAB stops doing whatever its doing and draws in the screen.
If you do
hold on
for ii=1:1000
plot(ii,rand(1)); % assume complicated maths here
end
MATLAB will run the code and send the plot calls to the graphics engine. However, MATLAB is too busy running the loop to be drawing, as the code has priority over the plot.
If you do
hold on
for ii=1:1000
plot(ii,rand(1));
drawnow; % Take a break, draw everything that you must before continuing
end
Then, as the comment says, you temporarily stop the execution of the code, to draw everything in the graphics pipeline, and then continue executing the code.
drawnow has no influence in the fact that the figure is stored or not, that is the job of hold on.
If you are worried about redrawing the whole thing, then make sure you have a look at set and get methods for graphics. With them you can get the xdata, modify it, and set it again, by ensuring that the graphics engine does not redraw/recompute anything else.
Documentation for the hold function:
https://uk.mathworks.com/help/matlab/ref/hold.html

multiple eyediagrams on a single figure in MATLAB

Is there a way to have multiple eyediagrams on a single figure in MATLAB. I want to do something like this:
figure;
subplot(311);
eyediagram(x1, ....);
subplot(312);<br>
eyediagram(x2, ....);
subplot(313);
eyediagram(x3, ....);
Unfortunately each call to eyediagram creates its own plot. I already tried to plot multiple eyediagram and copy them individually to another figure but I was wondering if there is a better/cleaner way to do this.
Thanks!
From the documentation:
Note: You cannot use hold on to plot multiple signals in the same figure.
Based on that statement, and the fact that you can only specify a figure handle to eyediagram (see below) and not an axes handle, this is not possible apart from manually copying the plot objects as you have stated (likely using copyobj).
Specifying a Figure
eyediagram(x,n,period,offset,plotstring,h) is the same as the syntax above, except that the eye diagram is in the figure whose handle is h, rather than in a new figure. h must be a handle to a figure that eyediagram previously generated.

Suppress function display in matlab ezpolar

I am using ezpolar to draw an arc on a compass plot. It's super easy,
ezpolar('1', [TH1, TH2])
to plot a circular arc with a radius 1 over the range [TH1, TH2]. However the function itself "r=1" gets displayed on the plot, which I do not want. I can't find and references to how this can be suppressed with an extra parameter or something.
I realise I could plot the arc other ways, but this is a cool function and super-compact, so I'd like to use it here and in the future.
Anyone know how to suppress the displaying of the function on the plot?
Thanks in advance
Tricky!
It looks like ezpolar is one of those MATLAB plots where a lot of things happen but nothing gets returned to the user. However I found the way using findobj.
Fiddling with the plot I realized that it is a 'Text' object what MATLAB puts in the plot, so we can try to find it with
h = findobj(gca,'Type','Text')
and then delete the text, for example with:
h.String='';
Tada!

Redraw a figure saved in 2013b in 2014b

As MATLAB has changed its figure engine in R2014b I decided to rerun some of my code for getting better looking figures out of them. Unfortunately, the last one I have is a code that takes ages to run, and I would like to highly avoid to rerun the code for a nicer figure.
I saved the result in a .fig file in R2013b. However, if I open it in R2014b, it still has the old format.
Is it possible to redraw the figure using the MATLAB R2014b plotting engine? If it is, how could I do it?
NOTE: Literally, the figure is opened and drawn with the new engine, however, it retains its old format. While a new figure with a title() command would plot a nice big, bold title, if a redraw this figure using "drawnow" or I generate code for it, the format remains the same.
Example: This figure was created in 2013b, and redrawn in 2014b. You can see that the title does not plot in the same format as a title('whatever') would plot in the new graphic handles. It looks like that a '.fig' saves and sets the default values for the version it has been generated. Thus plot colors, titles, labels etc will look like the old graphic handles when redrawn.
This can be tested with the following code. Note that this is an overly simplified problem, the question is not explicitly about titles or labels, but all graphic stuff in general.
rng(1)
figure()
x = 1:50;
y = rand(1, 50);
plot(x,y)
title('this NICE Title')
xlabel('labels!')
ylabel('some other labels','Interpreter','Latex')
If this code is run in 2013b and 2014b, saved as fig in both and then opened as fig in both, the next 2 figures appear:
The 2013b fig file: http://s000.tinyupload.com/index.php?file_id=02053933004513599550
There is a roundabout way for doing this -- just using hgopen for loading the figure and then extracting the data to re-plot it in 2014b:
h1=hgopen('test.fig'); % h1 = handle to the figure
allaxes=get(h1,'children'); % allaxes = array with axes handles
for a=1:length(allaxes)
ax=allaxes(a);
allines=get(ax,'children'); % all lines in current axes
for l=1:length(allines)
lin=allines(l);
values=get(lin,'ydata'); % values of the current line
subplots{a}{l}=values;
end
end
You can then use the subplots cell array to make the plots again by hand. It is a boring way to do it, but may be worth trying if re-generating the output takes very long.