plotting multiple series on one graph from a text file
4 views (last 30 days)
Show older comments
ciaran balfe
on 11 Feb 2019
Commented: ciaran balfe
on 11 Mar 2019
i am trying to make a single graph from a text file which is looped with 15 different variables over time.
does anyone know how i can do this? below is an attachment of the text file im attempting to graph.
column one repeats itself 1-15 and i wish for each integer from 1-15 to have its own series on the graph
0 Comments
Accepted Answer
Star Strider
on 11 Feb 2019
They all appear to plot together, and there is no easy way to offset them to plot them in a single plot. The best option then appears to be plotting them each in a subplot:
fidi = fopen('carpos.txt','rt');
Dc = textscan(fidi, '%f%f%f', 'CollectOutput',1);
D = cell2mat(Dc);
endData = find(D(:,1) == 14, 1, 'last');
D = D(1:endData,:);
Dr = reshape(D, 15, 3, []);
figure
for k1 = 1:size(Dr,1)
subplot(5,3,k1)
plot3(squeeze(Dr(k1,1,:)), squeeze(Dr(k1,2,:)), squeeze(Dr(k1,3,:)))
grid on
axis equal
title(sprintf('%d',k1-1))
end
Experiment to get the result you want.
7 Comments
Star Strider
on 23 Feb 2019
You need to tell textscan to ignore the
column (since I believe that is what you want to do). Do that by adding a ‘%*f’ to the end of the format string:
fidi = fopen('pos2.txt');
Dc = textscan(fidi, '%f%f%f%*f', 'CollectOutput',1);
fclose(fidi);
D = cell2mat(Dc);
ExptVct = unique(D(:,1)); % ‘Experiments’ Vector (Assumes All Data Sets Have The Same Number Of Experiments)
RowLim = floor(size(D,1)/numel(ExptVct))*numel(ExptVct); % Row Limit Of Data Sets With Complete Experiments
D = D(1:RowLim,:); % Trim ‘D’ To Data Sets With Complete Experiments
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 (10^{-1} s)')
title('Car Position')
legend(compose('%2d',ExptVct), 'Location','NW')
producing:
%20-%202019%2002%2023.png)
This looks like the previous plots, so I’m guessing that I ignored the correct (
) column of repeated ‘2’ values.
Experiment to get the result you want.
More Answers (0)
See Also
Categories
Find more on Annotations in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!