Why Matlab is not displaying all the figures?

258 views (last 30 days)
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 Comments
louis ferreira
louis ferreira on 2 Jul 2021
Edited: louis ferreira on 2 Jul 2021
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 on 2 Jul 2021
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.

Sign in to comment.

Accepted Answer

the cyclist
the cyclist on 13 Oct 2015
Edited: the cyclist on 13 Oct 2015
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 Comments
Image Analyst
Image Analyst on 13 Oct 2015
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 on 13 Oct 2015
Thanks Image Analyst! I did not see your post at first. But it did solve the issue.

Sign in to comment.

More Answers (1)

Image Analyst
Image Analyst on 13 Oct 2015
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);

Categories

Find more on Genomics and Next Generation Sequencing in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!