Control figure title, size and position in for loop
8 Ansichten (letzte 30 Tage)
Ältere Kommentare anzeigen
태신 김
am 22 Nov. 2021
Kommentiert: Walter Roberson
am 23 Nov. 2021
Hi all,
I want to express the changing Gaussian beam along the z values.
There is a for loop with pause() function for the animation.
The figure is changed with each loop.
But there are two problems.
1. How to add the title of the whole figure, not subtitles?
2. As soon as I execute the code, the figure frame is created that I intended, but instantly, the figure size is changed to small, and it is placed at not intended position.
Here is the code.
clc; clear all; close alll;
DataSet = zeros(1200,1600,30);
x = linspace(-30,30,1600);
y = linspace(-30,30,1200);
z = linspace(100,10000000,30);
[X Y] = meshgrid(x,y);
lambda = 532*10^(-6);
w0 = 16;
wz = w0*sqrt(1+(lambda*z/(pi*w0^2)).^2);
I0 = 1;
FigH = figure;
set(FigH,'Position',[1000 0 600 900])
for k = 1:5:30
printf('position z: %d / %d \n',z(k),max(z))
DataSet(:,:,k) = I0*(w0/wz(k))^2*exp(-2*(X.^2+Y.^2)/(wz(k)^2));
subplot(3,2,1)
p1 = imagesc(x,y,DataSet(:,:,k));
colormap jet
axis equal
title('Subplot 1')
subplot(3,2,2)
p2 = imagesc(x,y,DataSet(:,:,k));
colormap jet
axis equal
title('Subplot 2')
subplot(3,2,3)
p3 = imagesc(x,y,DataSet(:,:,k));
colormap jet
axis equal
title('Subplot 3')
subplot(3,2,4)
p4 = imagesc(x,y,DataSet(:,:,k));
colormap jet
axis equal
title('Subplot 4')
subplot(3,2,5)
p5 = imagesc(x,y,DataSet(:,:,k));
colormap jet
axis equal
title('Subplot 5')
subplot(3,2,6)
p6 = imagesc(x,y,DataSet(:,:,k));
colormap jet
axis equal
title('Subplot 6')
pause(0.00000001)
end
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/809154/image.png)
Any ideas? Thx in advance. ;-)
4 Kommentare
Walter Roberson
am 23 Nov. 2021
"There is no problem asking a question about Octave here"
This is a MATLAB / Mathworks forum, Octave is off-topic here, and any question which is specific to Octave may be closed as being irrelevant to MATLAB Answers.
Reminder: the purpose of Octave, the reason it exists, is to try to force Mathworks to either open-source its software... or to shut down the company. Either possibility would be considered a success for the Free Software Foundation.
The Free Software Foundation does not exist to provide high-quality affordable software to people who might otherwise not be able to afford it. The Free Software Foundation is a political entity whose purpose is to try to force the end of proprietary software, and its production of open-source software is a tactic rather than an end of its own.
Akzeptierte Antwort
Mathieu NOE
am 22 Nov. 2021
hello
see my suggestion below . I didn't have the problem of figure resizing, but I don't understand why you plot 6 times exactly the same data . I guessed that the 6 subplots should correspond to the 6 iteration steps , so I modified a bit your code to reflect that
for the main title, I used the info available here : How can I insert a title over a group of subplots? - (mathworks.com)
My result at the end of the for loop
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/809234/image.png)
clc; clear all; close all;
DataSet = zeros(1200,1600,30);
x = linspace(-30,30,1600);
y = linspace(-30,30,1200);
z = linspace(100,10000000,30);
[X Y] = meshgrid(x,y);
lambda = 532*10^(-6);
w0 = 16;
wz = w0*sqrt(1+(lambda*z/(pi*w0^2)).^2);
I0 = 1;
FigH = figure;
set(FigH,'Position',[1000 0 600 900])
sgtitle('Subplot Grid Title') % create main title
% see : https://fr.mathworks.com/matlabcentral/answers/100459-how-can-i-insert-a-title-over-a-group-of-subplots
for ci = 1:6
k = 1+5*(ci-1);
sprintf('position z: %d / %d \n',z(k),max(z))
DataSet = I0*(w0/wz(k))^2*exp(-2*(X.^2+Y.^2)/(wz(k)^2));
subplot(3,2,ci)
p1 = imagesc(x,y,DataSet);
colormap jet
axis equal
title(['Subplot ' num2str(ci)])
pause(0.25)
end
Weitere Antworten (0)
Siehe auch
Kategorien
Mehr zu Blue finden Sie in Help Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!