how can i plot this data?

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)

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:
how can i plot this data (1) - 2019 03 11.png
and:
how can i plot this data (2) - 2019 03 11.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

there are 2 lanes in the picture now, hopefully no casualties! thank you again for your reply but i dont understadn what i should change in the orignal code. ive tried changing a few things but nothing worked for me
i think i got it thank you. but i ran into another problem. when i wish to add more cars to it, so more data in the text file, it goes back to not working again, do you know why that is?
unfortunately i cant upload the text file as it is too large
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.
does your code still work with the following text files? i cant seem to get it to work with larger text files. the text files had to be a zip file due to exceeding 5mb limit on this
thank you
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.
hi sorry to bother you again. i made an error in the files i sent you originally, my appologies. cars are meant to 'switch lane' if so desireable so if you look at these text files you will notice that car '2' in 'carpos' after a a time of roughly 29.8 seconds switches files to 'carpos2' and you will see that in the 'carpos2' file at time 29.9 seconds. This is what is distorting the matlab code for graphing.
the text files i sent you i didnt have enough cars in the simulation for a car to have the desire to change lane, i was trying to shorten the memory so i could post this with ease. this is also why the zip coded files wont work, as they have many cars that switch lane or carpos to carpos2.
if you could check this out i would greatly appreciate it
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
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!
sorry i meant to say in column 1, if you look at the data you posted from the text file, it skips '2' and reads 1, 3, 4, 5...etc this is because car number '2' has gone to the other file, esentially, car '2' has changed lanes to the other file. this is what i think the probem im having is now
hi im sory to bother you again but i think i know why my other files wont work, it messes with your call to Dr = reshape(D, numel(ExptVct), [], 3); as in the new files more additions to column 1 are being added in as time goes on or as the text file progresses and this messes with the arrays produced in your 'Dr'. is there any way of completing the same plot without having this 'Dr' part of the code? thanks!
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.

Asked:

on 11 Mar 2019

Closed:

on 20 Aug 2021

Community Treasure Hunt

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

Start Hunting!