how can i plot this data?
Info
This question is closed. Reopen it to edit or answer.
Show older comments
i have this data from a text file that i want to plot like the supplied image, can anyone help me please?
here is the code i used to graph the picture shown
fidi = fopen('carpos2.txt','rt');
Dc = textscan(fidi, '%f%f%f%*f', 'CollectOutput',1);
fclose(fidi);
D = cell2mat(Dc);
ExptVct = unique(D(:,1));
RowLim = floor(size(D,1)/numel(ExptVct))*numel(ExptVct);
D = D(1:RowLim,:);
Dr = reshape(fidi = fopen('carpos2.txt','rt');
Dc = textscan(fidi, '%f%f%f%*f', 'CollectOutput',1);
fclose(fidi);
D = cell2mat(Dc);
ExptVct = unique(D(:,1));
RowLim = floor(size(D,1)/numel(ExptVct))*numel(ExptVct);
D = D(1:RowLim,:);
Dr = reshape(D, numel(ExptVct), [], 3);
figure
hold all
for k1 = 1:size(Dr,1)
plot(squeeze(Dr(k1,:,2)), squeeze(Dr(k1,:,3)))
end
hold off
grid
xlabel('Simulation Time (s)')
ylabel('velocity (m/s)')
%%title('Car ')
%%legend(compose('%2d',ExptVct), 'Location','NW')D, numel(ExptVct), [], 3);
figure
hold all
for k1 = 1:size(Dr,1)
plot(squeeze(Dr(k1,:,2)), squeeze(Dr(k1,:,3)))
end
hold off
grid
xlabel('Simulation Time (s)')
ylabel('velocity (m/s)')
%%title('Car ')
%%legend(compose('%2d',ExptVct), 'Location','NW')
Answers (1)
Star Strider
on 11 Mar 2019
My code works just as well now as it did earlier, alghout you need to change this assignment back to the way I had it originally, from:
Dc = textscan(fidi, '%f%f%f%*f', 'CollectOutput',1);
that will not work for more reasons than I have time to enumerate just now, to:
Dr = reshape(D, numel(ExptVct), [], 3);
With these corrections, my code produces:
%20-%202019%2003%2011.png)
and:
%20-%202019%2003%2011.png)
I numbered them sequentially to accommodate the earlier image plots I have stored for this project. These are ‘carpos2’ and ‘carpos’ respectively in your current post.
Note that the curves cross, so if the cars are in the same lane, it is best to notify the local constabulary and appropriate insurance firms, and hope that no one gets hurt!
11 Comments
ciaran balfe
on 11 Mar 2019
ciaran balfe
on 11 Mar 2019
Star Strider
on 11 Mar 2019
My pleasure.
Make that one change, and my code should work.
The code I used this time:
filename{1} = 'carpos3';
filename{2} = 'carpos4';
plottitle{1} = 'Carpos 3';
plottitle{2} = 'Carpos 4';
k2 = 1;
fidi = fopen(sprintf('ciaran balfe %s.txt',filename{k2}), 'rt');
Dc = textscan(fidi, '%f%f%f%*f', 'CollectOutput',1);
fclose(fidi);
D = cell2mat(Dc);
ExptVct = unique(D(:,1));
RowLim = floor(size(D,1)/numel(ExptVct))*numel(ExptVct);
D = D(1:RowLim,:);
Dr = reshape(D, numel(ExptVct), [], 3);
figure
hold all
for k1 = 1:size(Dr,1)
plot(squeeze(Dr(k1,:,2)), squeeze(Dr(k1,:,3)))
end
hold off
grid
xlabel('Simulation Time (s)')
ylabel('velocity (m/s)')
%%title('Car ')
%%legend(compose('%2d',ExptVct), 'Location','NW')D, numel(ExptVct), [], 3);
figure
hold all
for k1 = 1:size(Dr,1)
plot(squeeze(Dr(k1,:,2)), squeeze(Dr(k1,:,3)))
end
hold off
grid
xlabel('Simulation Time (s)')
ylabel('velocity (m/s)')
title(plottitle{k2})
I checked it again just now to be sure it works. It does.
I saved them as ‘ciaran balfe carpos3.txt’ and ‘ciaran balfe carpos4.txt’ respectively (so as not to overwrite the earlier files, and so I can find them easily). Make the necessary changes with respect to your filenames and the fopen sprintf calls to plot them.
ciaran balfe
on 11 Mar 2019
Star Strider
on 12 Mar 2019
It works, sort of. The files must be corrupt somehow in that they do not appear to have the same format as the others. They do produce plots if read with this textscan call:
Dc = textscan(fidi, '%f%f%f%*f', 'CollectOutput',1);
although the plots aren’t as well-defined as the others.
I will let you troubleshoot that, since it appears to be some sort of data or file problem. The files themselves seem to be the same format as the others, although they could contain non-standard characters that are causing problems. My code is predicated on all files being valid and having the same essential format. If they don’t, then my code won’t work with them.
ciaran balfe
on 12 Mar 2019
Star Strider
on 12 Mar 2019
The plot of your latest ‘carpos.txt’ (that I named ‘carpos7.txt’ looks really strange (even ignoring the fourth column). The third column of the last several lines are all 0, so that’s likely part of the problem, since the cars all appear to come to a screeching stop:
0 549.800000 17878.004890 0
1 549.800000 17291.187202 0
3 549.800000 16061.612290 0
4 549.800000 11253.881351 0
5 549.800000 10473.048720 0
6 549.800000 10402.384327 0
7 549.800000 6470.378925 0
8 549.800000 4969.844054 0
9 549.800000 4881.642854 0
10 549.800000 2439.252297 0
11 549.800000 2387.394337 0
12 549.800000 2320.365332 0
13 549.800000 898.446775 0
14 549.800000 855.370560 0
15 549.800000 740.120959 0
16 549.800000 0.000000 0
17 549.800000 0.000000 0
18 549.800000 0.000000 0
19 549.800000 0.000000 0
20 549.800000 0.000000 0
21 549.800000 0.000000 0
22 549.800000 0.000000 0
23 549.800000 0.000000 0
24 549.800000 0.000000 0
I have no idea how you want to handle that.
Your latest ‘carpos2.txt’ (that I named ‘carpos8.txt’) looks normal, again ignoring the fourth column.
I have no idea how to interpret your data. You simply asked me to figure out how to plot it so it looks the way you want it to, and I did.
Star Strider
on 12 Mar 2019
Edited: Star Strider
on 12 Mar 2019
My pleasure.
‘its the fact that in the first column '2' carpos, goes to carpos2’
I have no idea what that means.
If my Answer helped you solve your problem, please Accept it!
ciaran balfe
on 12 Mar 2019
ciaran balfe
on 15 Mar 2019
Star Strider
on 15 Mar 2019
The ‘Dr’ matrix is necessary for the code in order to plot your data the way you originally said you want it plotted. It is part of four different function calls that create the the matrix data from your original vector:
ExptVct = unique(D(:,1));
RowLim = floor(size(D,1)/numel(ExptVct))*numel(ExptVct);
D = D(1:RowLim,:);
Dr = reshape(D, numel(ExptVct), [], 3);
These restrict the number of rows of your matrix that are used to create ‘Dr’ in order that it has compatible dimensions.
It worked with your original data, and ist assumptions are based on them. So long as your subsequent data are compatible with those original data, my code should work with them as well. If they are not, I cannot guarantee that it will.
This question is closed.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!