Search code examples
matlabloopsmatlab-figuresubplot

MATLAB: subplot of one loop


I would like to plot the orbital velocity Vs Depth. This is successful with the following script:

clear all; clc; close all;

u = [ 
    0.1841    0.6168    0.6889    0.3531   -0.2019   -0.6549   -0.7366   -0.3882    0.1988;
    0.1627    0.5404    0.6010    0.3045   -0.1819   -0.5763   -0.6439   -0.3353    0.1799;
    0.1436    0.4732    0.5240    0.2623   -0.1639   -0.5071   -0.5628   -0.2895    0.1629;
    0.1266    0.4141    0.4566    0.2258   -0.1475   -0.4463   -0.4919   -0.2498    0.1475;
    0.1113    0.3622    0.3977    0.1941   -0.1328   -0.3927   -0.4299   -0.2154    0.1336;
    0.0977    0.3165    0.3461    0.1666   -0.1196   -0.3455   -0.3756   -0.1855    0.1211];

w = [
    0.3543    0.0801   -0.2553   -0.4510   -0.3879   -0.1006    0.2431    0.4419    0.3824;
    0.3055    0.0660   -0.2262   -0.3956   -0.3391   -0.0874    0.2129    0.3866    0.3351;
    0.2630    0.0538   -0.2005   -0.3472   -0.2966   -0.0760    0.1866    0.3385    0.2939;
    0.2262    0.0434   -0.1780   -0.3050   -0.2596   -0.0661    0.1635    0.2965    0.2579;
    0.1942    0.0345   -0.1583   -0.2680   -0.2273   -0.0576    0.1434    0.2598    0.2264;
    0.1666    0.0269   -0.1409   -0.2358   -0.1992   -0.0502    0.1258    0.2278    0.1990];


xx = 0; yy = 0;
scale = 0; p = 1;
dz = 2;
figure(1)
for d = 1:6;
%d
   dz = dz - 2;
   for i =1:9;
     quiver(xx, yy+dz, p*u(d,i),p*w(d,i),scale)
     hold on
   end
end    
grid on    
axis equal;     
axis([-1 1 -10.5 0.5])     
%subplot(1,2,1)    
%axis([-1 1 -4.5 0.5])     
%subplot(1,2,2)    
%axis([-1 1 -10.5 -5.5]) 

My question: how to separate this plot into two subplots?

subplot(1,2,1)    
axis([-1 1 -4.5 0.5])     
subplot(1,2,2)    
axis([-1 1 -10.5 -5.5]) 

Solution

  • You can do this simply by plotting half of the values in one subplot and the other half of the values in the other subplot. Since your interval is d=1:6, you can use if condition that plots values for d=1:3in one subplot and plot values for d=4:6 in the second subplot. i.e.

     if d<=3 subplot(1,2,1); axis([-1 1 -4.5 0.5]); grid on;  end 
     if d>3 subplot(1,2,2); axis([-1 1 -10.5 -5.5]); grid on; end
    

    So, overall your code will be:

    clear all; clc; close all;
    
    u = [ 
        0.1841    0.6168    0.6889    0.3531   -0.2019   -0.6549   -0.7366   -0.3882    0.1988;
        0.1627    0.5404    0.6010    0.3045   -0.1819   -0.5763   -0.6439   -0.3353    0.1799;
        0.1436    0.4732    0.5240    0.2623   -0.1639   -0.5071   -0.5628   -0.2895    0.1629;
        0.1266    0.4141    0.4566    0.2258   -0.1475   -0.4463   -0.4919   -0.2498    0.1475;
        0.1113    0.3622    0.3977    0.1941   -0.1328   -0.3927   -0.4299   -0.2154    0.1336;
        0.0977    0.3165    0.3461    0.1666   -0.1196   -0.3455   -0.3756   -0.1855    0.1211];
    
    w = [
        0.3543    0.0801   -0.2553   -0.4510   -0.3879   -0.1006    0.2431    0.4419    0.3824;
        0.3055    0.0660   -0.2262   -0.3956   -0.3391   -0.0874    0.2129    0.3866    0.3351;
        0.2630    0.0538   -0.2005   -0.3472   -0.2966   -0.0760    0.1866    0.3385    0.2939;
        0.2262    0.0434   -0.1780   -0.3050   -0.2596   -0.0661    0.1635    0.2965    0.2579;
        0.1942    0.0345   -0.1583   -0.2680   -0.2273   -0.0576    0.1434    0.2598    0.2264;
        0.1666    0.0269   -0.1409   -0.2358   -0.1992   -0.0502    0.1258    0.2278    0.1990];
    
    
    xx = 0; yy = 0;
    scale = 0; p = 1;
    dz = 2;
    figure(1)
    
    for d = 1:6;
       dz = dz - 2;
       for i =1:9;
         if d<=3 subplot(1,2,1); axis([-1 1 -4.5 0.5]); grid on;  end 
         if d>3 subplot(1,2,2); axis([-1 1 -10.5 -5.5]); grid on; end
         quiver(xx, yy+dz, p*u(d,i),p*w(d,i),scale)
         hold on; 
       end
    end