using struct to plot multiple set of data on multiple figures [figure properties]

So, I'm trying to plot multiple data set on multiple figures and it worked so far after I went through many answers posted on this forum. (thanks, everyone)
Although, When I was trying to add the title for the figure, the following code didn't work:
%% Plot
myFig = struct;
myFig(1).fig = figure(1);
clf(myFig(1).fig);
myFig(2).fig = figure(2);
clf(myFig(2).fig);
myFig(1).ax = axes('Parent', myFig(1).fig, 'NextPlot', 'add');
myFig(2).ax = axes('Parent', myFig(2).fig, 'NextPlot', 'add');
for j = 1:size(selectedData,3)
scatter(selectedData(:,1,j),selectedData(:,4,j),1,'Parent',myFig(1).ax)
scatter(selectedData(:,1,j),selectedData(:,6,j),1,'Parent',myFig(2).ax)
if j == 1
hold on
end
end
myFig(1).fig;
title('ABC vs. X')
myFig(2).fig;
title('DEF vs. X')
The result was that in there was no title in Figure 1 and the title for Figure 2 was 'DEF vs. X'.
If I comment out the last 2 lines, 'ABC vs. X' appears on Figure 2, which I thought it supposed to be on Figure 1.
However, when I changed the last 4 line to:
figure(1);
title('ABC vs. X')
figure(2);
title('DEF vs. X')
Everything works as what I want.
My question is why doesn't myFig(1).fig or myFig(1) works when I assigned figure(1) to them in the beginning?
Is there a way to include title using myFig(1).fig instead of figure(1)?

 Respuesta aceptada

Matt J
Matt J el 28 de Abr. de 2021
Editada: Matt J el 28 de Abr. de 2021
My question is why doesn't myFig(1).fig or myFig(1) works ?
Neither of these are commands.
Is there a way to include title using myFig(1).fig instead of figure(1)?
The most direct solution:
title(myFig(1).ax,'ABC vs. X')
title(myFig(2).ax,'DEF vs. X')

5 comentarios

Thank you very much! And do you know why myFig(1).fig didn't work? I thought it's just the same as figure(1).
Matt J
Matt J el 28 de Abr. de 2021
Editada: Matt J el 28 de Abr. de 2021
No, figure(1) is a command. It instructs Matlab to create a figure, or to make figure no. 1 current, if it already exists.
myFig(1).fig is just a variable that happens to hold a handle to a figure.
yoohooo's
yoohooo's el 28 de Abr. de 2021
Editada: yoohooo's el 28 de Abr. de 2021
Ahh, thank you very much!
And why .ax instead of .fig? I thought .ax is should be used when I want to add a new plot. And .fig is calling the figure.
A figure can contain many axes. title() needs to be told which axis in the figure to place the title in.
Thank you very much!

Iniciar sesión para comentar.

Más respuestas (0)

Categorías

Más información sobre Creating, Deleting, and Querying Graphics Objects en Centro de ayuda y File Exchange.

Preguntada:

el 28 de Abr. de 2021

Comentada:

el 28 de Abr. de 2021

Community Treasure Hunt

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

Start Hunting!

Translated by