Reading all rows of a csv file and plotting data continuously on one plot
Show older comments
I have a csv file containing 71588 rows and 15 columns of data. The first three colums are site number, location, and date of the data. The following 12 columns are sea level height measurements. Each row represents a 12 hour time period, with the latter 12 columns being a height measurement for hours 1 through 12. The 71588 rows represent hourly height measurements over approximately 98 years. I would like to make a plot of one continuous line representing sea level height. I need to write a script in Matlab which reads each row of hourly data and plots it continuously as if each seperate row were one continuous row of height measurements. Currently the script I have reads each row and plots the 12 hours of data stacked on one x location, then the next 12 hours stacked on the next x location, so on. Time is on the x axis and height being on the y. I'm assuming I have to use some type of loop to read and plot each row continuously until the 71588th row. This isn't something I have epereince with and I couldn't find any other threads showing a similar enough problem for me to understand.
Here is my code:
%plot Boston hourly 1921-2018
clear all, close all
%YMDHG = YearMonthDayHourGroup - format xxxxxxxxx
%HT = All heights
[Station,City,YMDHG,H01,H02,H03,H04,H05,H06,H07,H08,H09,H10,H11,H12] = textread('Hourly_data_1921_2018_edited.csv', '%s %s %f %f %f %f %f %f %f %f %f %f %f %f %f', 71588,'delimiter',',','emptyvalue', NaN);
HT=[H01,H02,H03,H04,H05,H06,H07,H08,H09,H10,H11,H12];
plot(YMDHG,HT);
xlabel('Year'), ylabel('Tidal Stage (mm)'),
legend('Sea Surface Height')
Here is an image of the output, each color being a different hour (one through twelve), stacked on one x location:

4 Comments
darova
on 25 Mar 2020
This is how i see you task

Bryce Mase
on 25 Mar 2020
Tommy
on 26 Mar 2020
Try:
HT=reshape([H01,H02,H03,H04,H05,H06,H07,H08,H09,H10,H11,H12]',[],1);
to get your heights in order. Then you'll need an array of the same size with the hours. YMDHG is only a 12th of the size. Additionally, it seems like YMDHG does not contain just years. You'll have to pull meaningful data from each value in YMDHG (assuming you have something like 201801011, this really means year 2018, month 01, day 01, and hour group 1).
Just
plot(HT)
will plot the heights in order. To get the X axis to read the exact dates/times, it'll require playing around with YMDHG.
Bryce Mase
on 26 Mar 2020
Answers (0)
Categories
Find more on Creating, Deleting, and Querying Graphics Objects 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!