MATLAB: Plotting data within a loop

datafor loopmatrix arraymatrix manipulation

Plotting data within a loop
I have a data in four columns
x, y, t, and ID
In this formar
1st column: X1,X2,X3,X4, X5, X6, X7, X8, X9…..Xn.
2nd column: Y1,Y2,Y3, Y4, Y5, Y7, Y8, Y9…..Yn
3rd is Time: 1,2,3,1,2,1,2,3,4,1,1…..n
4th ID: 1,1,1,2,2,3,3,3,3,4,5….
So whenever ID change the time start value. I want to plot each ID separately. For example: 1 track should be for ID=1, 2ndtrack for ID=2 and so on.
This is my code:
Tr is data file.
max1=max(tr(:,h)); %h column is ID column
for s=1:max1
for r=1:g
if tr(r,h)==s
scatter(tr(r,1),tr(r,2),5, 'filled','LineWidth',0.5)
hold on
end
end
end
This code has 2 problem. First too slow. 2nd I get scatter plot and each point is different color. I can not get tracks for each ID.

Best Answer

I'm not sure if I understand exactly what you're trying to do, but here goes.
nums = unique(tr(:,h));
for i = 1:length(nums)
figure(nums(i))
plot(tr(tr(:,h)==nums(i),x),tr(tr(:,h)==nums(i),time),tr(tr(:,h)==nums(i),y),tr(tr(:,h)==nums(i),time));
end
It's probably still going to be relatively slow because you're generating 23 different figures, and any graphical requirements take a fair amount of time in MATLAB.
Related Question