Selecting very specific data from a table using a for loop
Show older comments
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!
Accepted Answer
More Answers (1)
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
Categories
Find more on MATLAB in Help Center and File Exchange
Products
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!