How to write a loop to plot a given range of rows/values from a column text file?

Hello, the explanation is long for as much clarity as possible but the problem should be easy.
I am a beginner in Matlab and I am struggling a bit. I know exactly what I want to achieve but do not know how to use the correct syntax. Here is my problem:
I have a data table in .txt format, with 3 columns.
- The first, x, being the YEAR column. From year 2000 to 2016 (17 years of data). Where there are 365 rows of “2000” , followed beneath it by 365 rows of 2001, and so on until 2016.
- The second, y, is the DAY OF THE YEAR column. Starting from day 1 to day 365, and repeating itself in this same column every time a new year starts/or 365 was reached.
- The third, z, is the DATA column.
See image to have an idea of what it looks like.
My goals are to plot the following:
1) yearly trends for a defined range of days, from day 91 to day 274 for every year.
2) daily trends for a defined DAY of the year for all the years
For 1) , I want to have a loop, where for each year in column 1 (17 years), will plot my data from column 3, only for a set range of days from column 2, from day 91 to day 274 ONLY.
For example, for year 2000, scan day 1 to 365, only select days between 91 and 274, and plot y vs. z for this defined range, save the plot as “year_2000.img”. Which will give me the yearly trend plot for year 2000 for thos specific days. And so on until 2016. So I should end up with 17 different plots.
Once I have this, I would like to make relevant comparisons, for example between each years , then I would only have to click in the images and be able to see in a quantitative way where are the biggest differences between the years etc, is it possible to incorporate this into the loop?
Here is what I got so far:
For 2) I want to make a loop where I pick a day from that same range, and plot the data value for that day for each year, which will lead to a plot like for example : data of day #100 vs. years (2000 to 2016).
The loop would go over the same day # for all the years, do and save the plot, then move on the next day, do and save the plot, etc until it finishes with the range of days I need the plots from. The range here will be the same, from day 91 to day 274, which means I will end up with 183 daily plots, spanning years 2000 to 2016.
I have not written a code for this yet as I do not know how to incorporate the range of data to be looped.
So my question is, how do I write these loops when column 1 has many rows for the same year, and column two has ordered and repeated values where I wish to only use a defined portion of it for every time its repeated until the bottom end of the table?
Thank you for reading so far and for helping if this is easy peasy for you!
Best,
Laura A.

1 commentaire

jonas
jonas le 17 Juil 2018
Modifié(e) : jonas le 17 Juil 2018
I don't think you need a loop. It would help a lot if you attach the data (or 2-3 years of data minimum).
Also, I don't see the point in saving images and comparing the trends qualitatively. It's easy, but you might aswell do it in MATLAB. Or better, compare them quantitatively, using various metrics.

Connectez-vous pour commenter.

Réponses (1)

First, I would recommend that you load your data into a table rather than using the old fashioned textscan. Tables are much easier to manipulate. All you'd have to do is:
dailydata = readtable(yourfile, 'Headerlines', 23, 'ReadVariableNames', false);
dailydata.Properties.VariableNames = {'Year', 'Day', 'Data'});
Note that if the 23rd header line actually contains the name of the columns, then readtable could automatically use that:
dailydata = readtable(yourfile, 'Headerlines', 22); %get column names from line 23
You can then easily get rid of the days that do not interest you:
filtereddata = dailydata(dailydata.Day >= 91 & dailydata.Day <=274, :); %only keep days 91 to 274
After that you can do your plotting with a loop or with splitapply and a helper function. Either way use findgroups to identify years/days
Loop option:
%plot by year:
[group, year] = findgroups(filtereddata.Year);
for g = 1:numel(year)
plot(filtereddata.Day(group == g), filtereddata.Data(group == g));
title(sprintf('Plot for year %d', year(g)));
%... set limits, etc
saveas(gcf, sprintf('year_%d', year(g)));
end
%plot by day:
[group, day] = findgroups(filtereddata.Day);
for g = 1:numel(day)
plot(filtereddata.Year(day == g), filtereddata.Data(day == g));
title(sprintf('Plot for day %d', day(g)));
%...
saveas(gcf, sprintf('day_%d', day(g)));
end

Catégories

En savoir plus sur Characters and Strings dans Centre d'aide et File Exchange

Question posée :

le 17 Juil 2018

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by