plotting multiple series on one graph from a text file
You are now following this question
- You will see updates in your followed content feed.
- You may receive emails, depending on your communication preferences.
An Error Occurred
Unable to complete the action because of changes made to the page. Reload the page to see its updated state.
Show older comments
1 vote
Share a link to this question
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
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 11 Feb 2019
You didn’t originally say how you wanted them plotted. I took my best shot.
I needed to restate the reshape call to calculate ‘Dr’ to do what you want.
The revised full code is now:
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
hold all
for k1 = 1:size(Dr,1)
plot(squeeze(Dr(k1,:,2)), squeeze(Dr(k1,:,3)))
end
hold off
grid
legend(compose('%2d',0:14), 'Location','NW')
xlabel('Simulation Time (10^{-1} s)')
title('Car Position')
legend(compose('%2d',0:14), 'Location','NW')
Use the sprintfc (undocumented) function if you do not have compose. The arguments are the same.
Experiment to get the result you want.
Star Strider
on 11 Feb 2019
Edited: Star Strider
on 11 Feb 2019
I tested this revision with your new file.
With two experiments, the ‘Dr’ assignment sould be:
Dr = reshape(D, 2, [], 3);
To automate it so that my code will automatically scale for any number of experiments, substitute these lines for the one ‘Dr’ assignment:
NrExpts = unique(D(:,1));
Dr = reshape(D, numel(NrExpts), [], 3);
The rest of my code automatically uses the size of ‘Dr’ to change the plot and legend entries.
The revised code is now:
fidi = fopen('carpos(2).txt','rt');
Dc = textscan(fidi, '%f%f%f', 'CollectOutput',1);
fclose(fidi);
D = cell2mat(Dc);
NrExpts = unique(D(:,1));
Dr = reshape(D, numel(NrExpts), [], 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',0:14), 'Location','NW')
If my Answer helped you solve your problem, please Accept it!
Star Strider
on 13 Feb 2019
Some of your files do not have the complete set of experiments. My code (different versions) ran without error with the files you posted.
This revision should work for all your files, although it will not use the ending data sets in your files that do not have the complete set of experiments, because those are incompatible with the reshape function.
The revised code is now:
fidi = fopen('carpos.txt','rt');
Dc = textscan(fidi, '%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')
I tested this with both ‘carpos(1).txt’ (the original file) and ‘carpos(2).txt’ the second file you provided. It now also uses ‘ExptVct’ for the legend in the plot.
You could turn this into a function if you want, with the only argument being the file name. That would be:
function carposplot(filename)
fidi = fopen(filename,'rt');
Dc = textscan(fidi, '%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')
end
Save it as ‘carposplot.m’ to a directory in your MATLAB search path. Then call it as:
carposplot('carpos.txt')
It has no outputs, although you could return some of its calculations if you want, by defining the function to have those outputs.
I tested the function with both of your files, and it ran without error with each.
ciaran balfe
on 13 Feb 2019
perfect that works thank you !
Star Strider
on 13 Feb 2019
My pleasure.
If my Answer helped you solve your problem please Accept it!
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.
ciaran balfe
on 11 Mar 2019
hi thank you again for your help, i hope you dont mind me asking another question. i have 2 output files and the are crossing over in data. i basically am looking for the exact same graph as you have kindly shown me beofre but it isnt graphing properly. ive attached a text file im trying to graph. thank you again. (fyi you were completely correct about ignoring the 4th column, its to be ignored in this one too).
More Answers (0)
Categories
Find more on MATLAB in Help Center and File Exchange
Tags
See Also
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!Select a Web Site
Choose a web site to get translated content where available and see local events and offers. Based on your location, we recommend that you select: .
You can also select a web site from the following list
How to Get Best Site Performance
Select the China site (in Chinese or English) for best site performance. Other MathWorks country sites are not optimized for visits from your location.
Americas
- América Latina (Español)
- Canada (English)
- United States (English)
Europe
- Belgium (English)
- Denmark (English)
- Deutschland (Deutsch)
- España (Español)
- Finland (English)
- France (Français)
- Ireland (English)
- Italia (Italiano)
- Luxembourg (English)
- Netherlands (English)
- Norway (English)
- Österreich (Deutsch)
- Portugal (English)
- Sweden (English)
- Switzerland
- United Kingdom (English)