Loops in Matlab + Multiple Plots

Hi, I'm new to MATLAB, I have a piece of code that is working that takes computed currents of linear antennas and plots them, it looks like: (each antenna is divided into M pieces)
I1=abs(I(M+1:end,1)); z1=z(M+1:end,1);
I2=abs(I(M+1:end,2)); z2=z(M+1:end,2);
I3=abs(I(M+1:end,3)); z3=z(M+1:end,3); etc.
figure; plot(z1,I1,'.'); figure; plot(z2,I2,'.'); etc.
What I want to do is automate this for an arbitrary number of antennas. I have had several trials of the sort:
for j=1:N
I_(j)=abs(I(M+1:end,j)); z_(j)=z(M+1:end,j);
end
..or, I_j, or I(j) .. but as it became clear, I need to understand first what's going on in this (M+1:end,1) statement, and why do I get errors when I try any of the trial codes above.
1st type of error, for I_(j) and I(j)
??? In an assignment A(I) = B, the number of elements in B and I must be the same.
Error in ==> VB_Array25 at 18 I_(j)=abs(I(M+1:end,j)); z_(j)=z(M+1:end,j);
2nd type is when I use I_j, it doesn't say anything about the for loop but when I try figure; plot(z_1,I_1,'.');
??? Undefined function or variable 'z_1'.
Error in ==> VB_Array25 at 29 figure; plot(z_1,I_1,'.');
Any help will be more than welcome. -----------------------------------------------------
What I basically want to do after this, is be able to make all plots of I(z) for an arbitrary number of antennas on one plot and I will appreciate any comments on this as well.

 采纳的回答

Use cell arrays.
for jj=1:N
Ic{jj} = abs(I(M+1:end,jj));
z{jj} = z(M+1:end,jj);
figure
plot(z{ii},Ic{ii})
end
As for the (M+1:end,jj) statement, this just means to take the M+1 through last rows, and the jjth column. You might also want to take a look at the SUBPLOT function, rather than making all those figures.

3 个评论

Thanks, that worked.
I have taken a look at Subplot but it seems that it can only fit 4 plots at once, is there a way to fit any number?
I don't know where you got that idea. From the help for SUBPLOT (first line in fact) -
H = SUBPLOT(m,n,p), or SUBPLOT(mnp), breaks the Figure window
into an m-by-n matrix of small axes, selects the p-th axes for
the current plot, and returns the axis handle.
Did you read the help for SUBPLOT??
I am sorry, that was a stupid decision to look else than the help files, I swear I read somewhere that it does only 2x2. Thanks.

请先登录,再进行评论。

更多回答(0 个)

Community Treasure Hunt

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

Start Hunting!

Translated by