I have a class method that I am using to plot some images. The images are passed in a vectors and reshaped into the image.
classdef MyClass
methods(Static)
function plot_image(x, shape, my_title)
figure
imshow(reshape(x, shape));
title(my_title, 'Fontsize', 16);
end
function plot_two_images(x, y, shape, my_titles)
figure
subplots(1,2,1)
imshow(reshape(x, shape));
title(my_titles(1), 'Fontsize', 16);
subplots(1,2,2)
imshow(reshape(y, shape));
title(my_titles(2), 'Fontsize', 16);
end
end
end
When I try plotting with
my_cls = MyClass
my_cls.plot_two_images(x,y,shape,{'1','2'})
my_cls.plot_image(x,shape,'3')
my_cls.plot_image(y,shape,'4')
the titles show up on the previous image. So, 3 shows up where 2 should be, 4 shows up where 3 should be and the 4th image has no title. Why would this be the case?

 Réponse acceptée

Tommy
Tommy le 3 Avr 2020

0 votes

For both imshow() and title(), you can specify a target axes:
f = figure;
ax = axes(f);
imshow(reshape(x, shape), 'Parent', ax);
title(ax, my_title, 'Fontsize', 16);

Plus de réponses (1)

Jonathan Wittmer
Jonathan Wittmer le 3 Avr 2020

0 votes

The issue seems to not have anything to do with calling functions or using classes as the problem persists even when putting the plotting code inline with the calling code.
It seems that there is a timing mismatch between when I am calling
figure()
plotting with
imshow()
and setting the title. The issue was resolved by putting pause statements after figure() and after title(). This seems like a silly workaround though... I shouldn't have to put pause statements in the code to make sure it gets executed in the order its called. If anybody has a better way to get this working right, I'd be happy to hear.

4 commentaires

Tommy
Tommy le 3 Avr 2020
Modifié(e) : Tommy le 3 Avr 2020
For both imshow() and title(), you can specify a target axes. What happens if you remove the pause statements and instead try this?
f = figure;
ax = axes(f);
imshow(reshape(x, shape), 'Parent', ax);
title(ax, my_title, 'Fontsize', 16);
Jonathan Wittmer
Jonathan Wittmer le 3 Avr 2020
Thanks! That made the program work as expected. If you put that as an answer, I will mark it as accepted.
Any idea why there would be a mismatch in the first place? I've never had a situation before where I needed to explicitly set the parent object for plotting or titles unless I needed to refer back to a previous figure somewhere later in the code.
Of course!
To be honest, I'm not sure... imshow(I) without the 'Parent' argument "displays the grayscale image I in a figure" according to the docs. I would assume it looks for gcf and then either finds the current axes in that figure if they exist or creates a new set of axes in that figure if not. title places your title in gca unless you explicitly specify the axes. It might be possible that gcf and gca do not update quickly enough after your calls to figure and imshow, respectively?
Something else to consider: a call to subplot creates a set of axes, meaning the call to imshow here:
figure
subplots(1,2,1)
imshow(reshape(x, shape)); % gcf already has axes
title(my_titles(1), 'Fontsize', 16);
is not quite the same as the call to imshow here:
figure
imshow(reshape(x, shape)); % gcf has no axes (until imshow creates them)
title(my_title, 'Fontsize', 16);
with regards to whether imshow needs to make a new set of axes. This is all just me throwing out ideas though, no real answer...
Dimitris Ampeliotis
Dimitris Ampeliotis le 22 Juil 2021
I had the exact same issue when drawing in subsequent figures. It seems that placing a drawnow statement before the commands that draw on the next figure, resolves the issue.

Connectez-vous pour commenter.

Catégories

En savoir plus sur Creating, Deleting, and Querying Graphics Objects dans Centre d'aide et File Exchange

Produits

Version

R2019b

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by