Question

i have following question and please help me to solve following task:

i want to create subplot in matlab which depend on loop variable,for example as i know to crate plot menu 2X2,we are doing like this

subplot(2,2,1)

subplot(2,2,2)

subplot(2,2,3)

subplot(2,2,4)

but can i do linear form?like 1:100?or something like this ,more generally like this

n=100;

for i=1:n

subplot(1,n,i)

?

thanks very much

EDITED CODE

function [order]=find_order(y,fs);
order=0;
n=length(y);
n1=nextpow2(n);
ndft=2^n1;
for i=1:floor(n/2)
    [Pxx,f]=pburg(y,i,ndft,fs);
    subplot(ndft,ndft,i);
    plot(f,Pxx);
    title(['order',num2str(i),'i']);
    order=i;    
end
end


   picture :

enter image description here

i can't understand what happens

Was it helpful?

Solution

1-D Demo

Code

%%// Data
t = 0:0.01:15*pi;
y1 = sin(t);

%%// Plot
figure,
for k=1:4
    subplot(4,1,k)
    plot(t((k-1)*1000+1:k*1000),y1((k-1)*1000+1:k*1000))
    xlim([0 40])
end

Output

enter image description here

2-D Demo

Code

%%// Data
t = 0:0.01:15*pi;
y1 = sin(t);

%%// Plot
colors=['r' 'g' ; 'y' 'k'];

figure,
for k1=1:2
    for k2=1:2
        subplot(2,2,(k1-1)*2+k2)
        plot(t,y1,colors(k1,k2));
    end
end

Output

enter image description here

Hopefully these demos would guide to you something meaningful for your case.

OTHER TIPS

Yes, it is:

n=5;

for i=1:n

subplot(1,n,i)

end

gives

enter image description here

for pat=1: N % main loop

% Define the sublot grid
s1=3; % subplot rows
s2=3; % subplot columns

% find the figure number
fig_num=floor(pat/(s1*s2))+1 % Figure number

% Find the subplot number
sub_fig=mod(pat,s1*s2)   % subplot number

% correct for corners
if(sub_fig==0)
    sub_fig=s1*s2;
    fig_num=fig_num-1;
end

% plot something
figure(fig_num);
subplot(s1,s2,sub_fig) ;

plot(1,1) % plot something

end % of main loop

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top