Why Matlab is not displaying all the figures?
43 次查看(过去 30 天)
显示 更早的评论
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
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
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)
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
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
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.
更多回答(1 个)
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);

0 个评论
另请参阅
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!