Info

This question is closed. Reopen it to edit or answer.

how can i plot this data?

1 view (last 30 days)
ciaran balfe
ciaran balfe on 11 Mar 2019
Closed: MATLAB Answer Bot on 20 Aug 2021
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
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:
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
ciaran balfe
ciaran balfe on 15 Mar 2019
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!
Star Strider
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!