Selecting very specific data from a table using a for loop

10 Ansichten (letzte 30 Tage)
Matt Jolley
Matt Jolley am 25 Okt. 2014
Bearbeitet: dpb am 26 Okt. 2014
Hi struggling with some simple Matlab, I have a 49440x3 table created from excel data, with columns 'title' 'x' 'y', the 'x' column contains values starting at 0.25 incrementing by 0.25 until it reaches 154.5 in a loop. I want to put each set of 0.25:0.25:154.5 into separate tables with the associated 'title' and 'y'.
Using a for loop I tried:
vars={'title','x','y'};
n=1;
for n=n+618;
p=n+617;
T=leafdata(n:p,vars)
end
This obviously overwrites the new table because I dont know how to create a dynamic variable instead of 'T' I tried:
vars={'title','x','y'};
n=1;
x=1;
for n=n+618;
p=n+617;
T(x)=leafdata(n:p,vars)
x=x+1;
end
And also
vars={'title','x','y'};
n=1;
x=1;
for n=n+618;
p=n+617;
x=leafdata(n:p,vars)
x=x+1;
end
Neither work and I have also tried starting the for loop with
vars={'title','x','y'};
n=1;
for rows=leafdata.x
if rows==0.25
n=n+1;
end
n=leafdata(rows,vars)
end
I would prefer to use a loop that searches for the 0.25 if possible? Or make T change to T1, T2...Tn after each loop. But Any solutions to this would be very welcome!

Akzeptierte Antwort

dpb
dpb am 26 Okt. 2014
"I want to put each set of 0.25:0.25:154.5 into separate tables with the associated 'title' and 'y'."
I'd ask "why"? It's a very typical newbie idea in Matlab to build more variables (in this case tables) but when they're associated it's rarely really the best solution. I'd suggest using a 2D array instead and address the sequences by column.
N=154.5*4; % number in each set
leafdata.x=reshape(leafdata.x,N,[]);
will do the rearrangement; use same for the other variables. Then each group is addressed as (:,nGrp) for subscripting expression for arbitrary group nGrp.
"Using a for loop I tried:"
n=1;
for n=n+618;
p=n+617;
x=leafdata(n:p,vars)
The above does nothing; n+618 is simply a single value of 619 and p=618 so you try to address [619:618] which is an empty set since the second array index is less than the first. But, it's a nonproductive path down which to head as addressed by the last question...
"Or make T change to T1, T2...Tn after each loop"
This is the subject of an FAQ that basically says "don't do that!" --
  1 Kommentar
Matt Jolley
Matt Jolley am 26 Okt. 2014
The reason I want to split the data is because this has been collected from an electromechanical machine with 80 motors and each has been sampled from 0.25s to 154.5s and each y value stored in the next column is the position. The data is stored in the same three columns for all 80 motors. I want to plot each motors movement seperately and because I dont know how to plot the data seperately if its all in one table I wanted to seperate it and plot the data from individual tables. Any further hints?

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (1)

dpb
dpb am 26 Okt. 2014
Bearbeitet: dpb am 26 Okt. 2014
Sure...as said, if you do the reshape to columns, you'll end up with a column of T, X, and Y for each machine/motor. That's the most logical arrangement you could have -- to plot any given motor you simply write
mtrNum=input('Which motor do you wish to plot?');
plot(T(:,mtrNum), [X(:,mtrNum) Y(:,mtrNum)])
legend('X','Y')
xlabel('Time')
ylabel('Position')
title(num2str(mtrNum,'Motor %d Position vs. Time'))
ADDENDUM
NB: If the data are indeed collected at the same time for each group then you can simplify the final dataset further by keeping only one time vector as they're all redundant.
T=T(:,1); % keep only the one time vector
Then you can eliminate the subscripting for it everywhere it's referenced--
plot(T, [X(:,mtrNum) Y(:,mtrNum)])
Also, to show further the power of keeping such an arrangement instead of many variables, to get (say) the distance the motor has traveled--
[~,d]=bsxfun(@minus,cart2pol(X,Y),cart2pol(X(1,:),Y(1,:)));
for all 80 (or however many there are) at "one swell foop". bsxfun is a builtin function that does singleton expansion for vector/array operations. See
doc bsxfun % for the gory details

Kategorien

Mehr zu Characters and Strings finden Sie in Help Center und File Exchange

Produkte

Community Treasure Hunt

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

Start Hunting!

Translated by