How do I plot several averages at once?

1 view (last 30 days)
Attached is an excerpt of a much longer text file. There are 19 columns total, each row leads with a time stamp, there are 5 rows per instance of time. I need to plot the averages of those columns with their respective times. There should be a figure for the columns 4-18.
For example, the text will read:
%Plot.txt
01-11-17_04,14,09PM $VNINS,317667.094785,1973,8085,+035.478,+004.194,-000.878,+34.61265901,-118.07304581,+00744.505,+000.015,-000.019,+000.553,39.5,03.4,0.55*60
01-11-17_04,14,09PM $VNINS,317667.294783,1973,0085,+035.481,+004.196,-000.879,+34.61265898,-118.07304572,+00744.422,+000.004,-000.005,+000.595,38.7,03.3,0.55*63
01-11-17_04,14,09PM $VNINS,317667.494783,1973,0085,+035.486,+004.196,-000.880,+34.61265913,-118.07304562,+00744.318,+000.021,+000.005,+000.647,38.2,03.2,0.58*6D
01-11-17_04,14,09PM $VNINS,317667.694783,1973,0085,+035.494,+004.200,-000.880,+34.61265880,-118.07304613,+00744.238,-000.032,-000.061,+000.688,37.5,03.1,0.61*6F
01-11-17_04,14,09PM $VNINS,317667.894783,1973,0085,+035.505,+004.201,-000.879,+34.61265870,-118.07304591,+00744.160,-000.049,-000.026,+000.724,36.9,03.0,0.63*61
01-11-17_04,14,10PM $VNINS,317668.094783,1973,0085,+035.505,+004.201,-000.875,+34.61265891,-118.07304579,+00744.063,-000.017,-000.006,+000.767,36.4,02.9,0.63*6A
01-11-17_04,14,10PM $VNINS,317668.294784,1973,0085,+035.505,+004.200,-000.875,+34.61265901,-118.07304591,+00743.943,-000.005,-000.016,+000.818,36.0,02.9,0.65*6B
01-11-17_04,14,10PM $VNINS,317668.494784,1973,A085,+035.510,+004.201,-000.876,+34.61265930,-118.07304591,+00743.817,+000.032,-000.014,+000.867,35.8,02.8,0.65*1A
01-11-17_04,14,10PM $VNINS,317668.694784,1973,8085,+035.515,+004.202,-000.875,+34.61265898,-118.07304563,+00743.710,-000.015,+000.021,+000.907,35.2,02.8,0.66*6F
01-11-17_04,14,10PM $VNINS,317668.894784,1973,D085,+035.516,+004.203,-000.875,+34.61265886,-118.07304520,+00743.590,-000.033,+000.066,+000.949,34.8,02.7,0.66*14
I would need 14 separate figures, in each figure the x-axis is time (two instances) and the y-axis would be the average of the five instances of recorded data for that column.
Please include as many comments as possible so that I may understand everything aspect of the script, thank you!
  4 Comments
Max Mason
Max Mason on 28 Aug 2018
The data should not be in rtf formate, I have attached a txt file which should be formatted correctly. Thanks
Walter Roberson
Walter Roberson on 28 Aug 2018
Edited: Walter Roberson on 28 Aug 2018
I noticed that the pl.txt you attached is not in the same format as your original data. Your original data matched the format you posted including the $VNINS strings; the pl.txt does not have those strings.
The pl.txt you posted includes two columns that cannot be plotted, including the final column, which contains entries such as '0.55*6D' -- a floating point number followed by an asterisk followed by two hex digits.

Sign in to comment.

Accepted Answer

Walter Roberson
Walter Roberson on 28 Aug 2018
C = readtable('pl.txt');
hms = duration(C{:,1}, C{:,2}, C{:,3});
origcol = [4, 5, 7:width(C)-1];
Ctt = table2timetable(C(:,origcol), 'RowTimes', hms);
meanCtt = retime(Ctt, unique(hms), 'mean');
numcol = width(meanCtt);
x = meanCtt.Time;
for K = 1 : numcol
figure();
plot(x, meanCtt{:,K});
legend( sprintf('Column #%d', origcol(K)) );
end

More Answers (1)

Pierre845
Pierre845 on 27 Aug 2018
Hello,
First you need to work out how to transform your text data into a matrix; for that purpose use either csvread, dlmread or textscan with the right options (trial and error will make it, as the functions are easy to understand)
Then simply do your average for each column (mean function), and finally plot them with the function plot (if you call the function plot multiple times, use 'Hold On' to keep the figure display on).
  1 Comment
Max Mason
Max Mason on 28 Aug 2018
I am new to Matlab so please walk me through some of the technicals.
In previous scripts I had been using the following format:
fid = fopen('pl.txt');
C = textscan(fid,'%q','Delimiter',',','HeaderLines',Count);
fclose(fid);
Where the variable "Count" is a variable which is raised by one at the end of each cycle of the script. This format takes the txt file line by line.
This format does not seem very efficient and has had many problems with interpreting the data into figures which make coherent sense.
Given the attached txt file, what do you think is the most efficient method of transforming my data into a matrix?
Thank you!

Sign in to comment.

Categories

Find more on Dates and Time 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!