Why Matlab is not displaying all the figures?

조회 수: 37 (최근 30일)
Andrino Giardino
Andrino Giardino 2015년 10월 13일
댓글: Rik 2021년 7월 2일
Hi everyone,
I am running code for basically 5 figures, but only 3 are showing. The other 2 work fine if I run them individually. Can someone please help? Thanks in advance! :)
%Figure1
x = [0:pi/100:2*pi];
y1 = cos(x);
y2 = sin(x);
plot(x,y1,'-k',x,y2,'--b');
xlabel('x (pi)');
ylabel('y(x)');
title('Plot of y1=cos(x) and y2=sin(x)');
axis([0 6 -1 1]);
legend('y1=cos(x)','y2=sin(x)');
%Figure2
M = peaks(50);
figure;
mesh(M);
surf(M);
title('Peaks #2');
%Figure3
x = [-10:0.5:10];
y = [-10:0.5:10];
[xx,yy] = meshgrid(x,y);
r = sqrt(xx.^2+yy.^2);
zz = [cos(r)/2+sin(r)/2];
surf(xx,yy,zz);
title('Surface #3');
%Figure4
k = 5;
n = 2^k-1;
[x,y,z] = sphere(n);
c = hadamard(2^k);
figure;
surf(x,y,z,c);
colormap([1,1,0;0,1,1]);
axis equal;
title('Surface #4');
%Figure5
t=0:0.5:10;
s=0:0.5:10;
[tt,ss] = meshgrid(s,t);
r=5+sin(10*ss+5*tt);
surf(xx,yy,zz);
xx=[ss,tt];
yy=[ss,tt];
zz=[ss,tt];
xx=r*cos(ss)*sin(tt);
yy=r*sin(ss)*sin(tt);
zz=r*cos(tt);
surf(xx,yy,zz);
xlabel('x');
ylabel('y');
zlabel('z');
title('Miscellaneous Surfaces #5');
  댓글 수: 4
louis ferreira
louis ferreira 2021년 7월 2일
편집: louis ferreira 2021년 7월 2일
yeah must of pasted it in by accident, and didnt notice when i was going through it because i've been looking at the code for days. Never heard of brainfarts?
Your question seems akin to asking a dead person why they got into a car crash if they wished to live...
Rik
Rik 2021년 7월 2일
To improve your analogy: his question is like asking someone who got into a car crash why he wasn't wearing a seat belt.
You are using an editor with a lot of features, including a linter that automatically analyses your code and highlights issues. clear all only needs to exist once in your entire code-base: as part of a script that essentially restarts Matlab. Using it just to wipe old variables is complete over-kill, which is something mlint is warning you about.
info = checkcode('comment_1615298.m','-struct');
numel(info)
ans = 74
When I paste your code in the Matlab editor, 74 check-engine-lights turn on. You should deal with each of them. In general mlint is correct. In the very rare circumstance that it isn't, you can use %#ok to suppress the warning (or better: right-click the orange line and select 'suppress warning on this line'). That way you can still confirm the rest of your code passes the tests.

댓글을 달려면 로그인하십시오.

채택된 답변

the cyclist
the cyclist 2015년 10월 13일
편집: the cyclist 2015년 10월 13일
They are overwriting each other in the figure window. Use a figure command for each new figure.
For example,
if true
% code
end
%Figure1
x = [0:pi/100:2*pi];
y1 = cos(x);
y2 = sin(x);
figure
plot(x,y1,'-k',x,y2,'--b');
xlabel('x (pi)');
ylabel('y(x)');
title('Plot of y1=cos(x) and y2=sin(x)');
axis([0 6 -1 1]);
legend('y1=cos(x)','y2=sin(x)');
%Figure2
M = peaks(50);
figure;
mesh(M);
surf(M);
title('Peaks #2');
%Figure3
x = [-10:0.5:10];
y = [-10:0.5:10];
[xx,yy] = meshgrid(x,y);
r = sqrt(xx.^2+yy.^2);
zz = [cos(r)/2+sin(r)/2];
figure
surf(xx,yy,zz);
title('Surface #3');
%Figure4
k = 5;
n = 2^k-1;
[x,y,z] = sphere(n);
c = hadamard(2^k);
figure;
surf(x,y,z,c);
colormap([1,1,0;0,1,1]);
axis equal;
title('Surface #4');
%Figure5
t=0:0.5:10;
s=0:0.5:10;
[tt,ss] = meshgrid(s,t);
r=5+sin(10*ss+5*tt);
figure
surf(xx,yy,zz);
xx=[ss,tt];
yy=[ss,tt];
zz=[ss,tt];
xx=r*cos(ss)*sin(tt);
yy=r*sin(ss)*sin(tt);
zz=r*cos(tt);
surf(xx,yy,zz);
xlabel('x');
ylabel('y');
zlabel('z');
title('Miscellaneous Surfaces #5');
  댓글 수: 3
Image Analyst
Image Analyst 2015년 10월 13일
Did you overlook my answer below? I corrected that for you. If you want separate figures, just replace the subplot's in my code with calls to figure.
Andrino Giardino
Andrino Giardino 2015년 10월 13일
Thanks Image Analyst! I did not see your post at first. But it did solve the issue.

댓글을 달려면 로그인하십시오.

추가 답변 (1개)

Image Analyst
Image Analyst 2015년 10월 13일
Use subplot() instead so they can all go on one screen:
%Figure1
fontSize = 25;
x = [0:pi/100:2*pi];
y1 = cos(x);
y2 = sin(x);
subplot(2, 3, 1);
plot(x,y1,'-k',x,y2,'--b');
xlabel('x (pi)');
ylabel('y(x)');
title('Plot of y1=cos(x) and y2=sin(x)', 'fontSize', fontSize);
axis([0 6 -1 1]);
legend('y1=cos(x)','y2=sin(x)');
% Set up figure properties:
% Enlarge figure to full screen.
set(gcf, 'Units', 'Normalized', 'OuterPosition', [0 0 1 1]);
% Get rid of tool bar and pulldown menus that are along top of figure.
set(gcf, 'Toolbar', 'none', 'Menu', 'none');
% Give a name to the title bar.
set(gcf, 'Name', 'Demo by Andrino', 'NumberTitle', 'Off')
%Figure2
M = peaks(50);
subplot(2, 3, 2);
mesh(M);
surf(M);
title('Peaks #2', 'fontSize', fontSize);
%Figure3
x = [-10:0.5:10];
y = [-10:0.5:10];
[xx,yy] = meshgrid(x,y);
r = sqrt(xx.^2+yy.^2);
zz = [cos(r)/2+sin(r)/2];
subplot(2, 3, 3);
surf(xx,yy,zz);
title('Surface #3', 'fontSize', fontSize);
%Figure4
k = 5;
n = 2^k-1;
[x,y,z] = sphere(n);
c = hadamard(2^k);
subplot(2, 3, 4);
surf(x,y,z,c);
title('Surface #4', 'fontSize', fontSize);
colormap([1,1,0;0,1,1]);
axis equal;
%Figure5
t=0:0.5:10;
s=0:0.5:10;
[tt,ss] = meshgrid(s,t);
r=5+sin(10*ss+5*tt);
subplot(2, 3, 5);
surf(xx,yy,zz);
title('Surface #5', 'fontSize', fontSize);
xx=[ss,tt];
yy=[ss,tt];
zz=[ss,tt];
xx=r*cos(ss)*sin(tt);
yy=r*sin(ss)*sin(tt);
zz=r*cos(tt);
subplot(2, 3, 6);
surf(xx,yy,zz);
xlabel('x');
ylabel('y');
zlabel('z');
title('Miscellaneous Surfaces #6', 'fontSize', fontSize);

카테고리

Help CenterFile Exchange에서 2-D and 3-D Plots에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by