From: Anamika on
Hello,

How do you refer to the elements of an array that you load in a for loop?

In the code below, for the subplot section how can the variable be set so that it changes every time from X1 to X2 and so on until the loop gets over.

Thanks for your input!
~Mika

% Data file containing coordinates to be plotted

load MyDataSet.dat

% Loaded data gets assigned

X1 = MyDataSet(1:616,2); Y1 = MyDataSet(1:616, 3);
X2 = MyDataSet(617:1131,2); Y2 = MyDataSet(617:1131, 3);
X3 = MyDataSet(1132:1655,2); Y3 = MyDataSet(1132:1655, 3);
X4 = MyDataSet(1656:2050,2); Y4 = MyDataSet(1656:2050, 3);
.....
.....
.....
X20 = MyDataSet(6661:6900,2); Y20 = MyDataSet(6661:6900, 3);

% Sublot routine
for n=1:20
h(n)=subplot(20,1,n);
plot(X1,Y1, 'r'); % HOW TO REFER ARRAY ELEMENTS HERE?
hold on;
end
From: us on
"Anamika " <Anamika.Darwin(a)gmail.com> wrote in message <hu9eeq$9p8$1(a)fred.mathworks.com>...
> Hello,
>
> How do you refer to the elements of an array that you load in a for loop?
>
> In the code below, for the subplot section how can the variable be set so that it changes every time from X1 to X2 and so on until the loop gets over.
>
> Thanks for your input!
> ~Mika
>
> % Data file containing coordinates to be plotted
>
> load MyDataSet.dat
>
> % Loaded data gets assigned
>
> X1 = MyDataSet(1:616,2); Y1 = MyDataSet(1:616, 3);
> X2 = MyDataSet(617:1131,2); Y2 = MyDataSet(617:1131, 3);
> X3 = MyDataSet(1132:1655,2); Y3 = MyDataSet(1132:1655, 3);
> X4 = MyDataSet(1656:2050,2); Y4 = MyDataSet(1656:2050, 3);
> ....
> ....
> ....
> X20 = MyDataSet(6661:6900,2); Y20 = MyDataSet(6661:6900, 3);
>
> % Sublot routine
> for n=1:20
> h(n)=subplot(20,1,n);
> plot(X1,Y1, 'r'); % HOW TO REFER ARRAY ELEMENTS HERE?
> hold on;
> end

this is heart-breaking terrible...

http://matlabwiki.mathworks.com/MATLAB_FAQ#How_can_I_create_variables_A1.2C_A2.2C....2CA10_in_a_loop.3F

us
From: us on
"Anamika " <Anamika.Darwin(a)gmail.com> wrote in message <hu9eb2$2di$1(a)fred.mathworks.com>...
> Hello,
>
> How do you refer to the elements of an array that you load in a for loop?
>
> In the code below, for the subplot section how can the variable be set so that it changes every time from X1 to X2 and so on until the loop gets over.
>
> Thanks for your input!
> ~Mika
>
> % Data file containing coordinates to be plotted
>
> load MyDataSet.dat
>
> % Loaded data gets assigned
>
> X1 = MyDataSet(1:616,2); Y1 = MyDataSet(1:616, 3);
> X2 = MyDataSet(617:1131,2); Y2 = MyDataSet(617:1131, 3);
> X3 = MyDataSet(1132:1655,2); Y3 = MyDataSet(1132:1655, 3);
> X4 = MyDataSet(1656:2050,2); Y4 = MyDataSet(1656:2050, 3);
> ....
> ....
> ....
> X20 = MyDataSet(6661:6900,2); Y20 = MyDataSet(6661:6900, 3);
>
> % Sublot routine
> for n=1:20
> h(n)=subplot(20,1,n);
> plot(X1,Y1, 'r'); % HOW TO REFER ARRAY ELEMENTS HERE?
> hold on;
> end

don't dup OPs(!)...
the answer's here at

http://www.mathworks.com/matlabcentral/newsreader/view_thread/283836

us
From: dpb on
Anamika wrote:
> Hello,
>
> How do you refer to the elements of an array that you load in a for loop?

W/ subscripts such as

for idx = 1:length(x) % assume x is vector
z = abs(x(idx)); % do something w/ an element of x
end

> In the code below, for the subplot section how can the variable be set
> so that it changes every time from X1 to X2 and so on until the loop
> gets over.
....

> load MyDataSet.dat
>
> % Loaded data gets assigned
> X1 = MyDataSet(1:616,2); Y1 = MyDataSet(1:616, 3);
> X2 = MyDataSet(617:1131,2); Y2 = MyDataSet(617:1131, 3);
> X3 = MyDataSet(1132:1655,2); Y3 = MyDataSet(1132:1655, 3);
> X4 = MyDataSet(1656:2050,2); Y4 = MyDataSet(1656:2050, 3);
> ....
> ....
> ....
> X20 = MyDataSet(6661:6900,2); Y20 = MyDataSet(6661:6900, 3);
>
> % Sublot routine
> for n=1:20
> h(n)=subplot(20,1,n);
> plot(X1,Y1, 'r'); % HOW TO REFER ARRAY ELEMENTS HERE?
> hold on;
> end

I'd do something like (assuming you can't compute the breakpoints
desired but need them specified as such and I don't see a pattern there
otomh)...

x = mydataset; % Shorten var name just for demo/less typing...
blks = [615, 515, 524, ...]; % blocksize for each segment to plot
i1 = 1; % first point
for idx=1:length(blks)
h(idx)=subplot(20,1,idx); % probably find this too many, but...
i2 = i1+blks(idx); % end point this block
plot(x(i1:i2,2),x(i1:i2,3));
x1 = x2+1; % 1st next block one past end this one
end

--
From: Anamika on
Thanks dpb.

The breakpoints are entirely dependent on a each data set I deal with. Just one question, for the last line, I think you meant to re-initialize i1 and not x1 (typo?). When I changed and applied, I got plot I want.

Thanks,
Mika


>
> I'd do something like (assuming you can't compute the breakpoints
> desired but need them specified as such and I don't see a pattern there
> otomh)...
>
> x = mydataset; % Shorten var name just for demo/less typing...
> blks = [615, 515, 524, ...]; % blocksize for each segment to plot
> i1 = 1; % first point
> for idx=1:length(blks)
> h(idx)=subplot(20,1,idx); % probably find this too many, but...
> i2 = i1+blks(idx); % end point this block
> plot(x(i1:i2,2),x(i1:i2,3));
> x1 = x2+1; % 1st next block one past end this one
> end
>
> --