Converting to array index

2 次查看(过去 30 天)
Steven
Steven 2015-9-30
Okay so I am given a csv file with the a month, year and water level.
The first entry is January 1940 with say 50 water level and the last is 2015 august with 70 water level.
Now we are required to get a user to enter two different months and years and plot the water level between those two respective points. i.e for example between January 2000 and December 2000.
Now I imported all the data as follows:
Water = importdata('melbournewaterlevels.csv');
StrMonth = Water.textdata(2:end,2);
StrYear=Water.textdata(2:end,1);
WaterLevel = Water.data;
StrYear(1 : end, 1 : end);
Now prompting the user to enter there choice:
m1=input(' Please input Month 1: ');
y1=input(' Please input Year 1: ');
m2=input(' Please input Month 2: ');
y2=input(' Please input Year 2: ');
How can I get that for example to convert it into an index?

回答(1 个)

Andrei Bobrov
Andrei Bobrov 2015-9-30
编辑:Andrei Bobrov 2015-10-2
Please attach small part of the your file: melbournewaterlevels.csv.
date1 = [StrYear StrMonth];
level1 = Water.data;
ym1=input(' Please input first Year and Month as [Year, Month]: ');
ym2=input(' Please input last Year and Month as [Year, Month]: ');
l1 = datenum([ym1,1;ym2,1]);
d1 = datenum([date1,ones(size(date1,1),1)]);
out = WaterLevel(l1(1)>=d1 & l1(2)<=d1);
or
l1 = find(cumsum(ismember(date1, [ym1;ym2],'rows'))==1);
out = WaterLevel([l1;l1(end)+1]);
EDIT (after attaching of file)
Water = importdata('melbournewaterlevels.csv');
x = strcat(Water.textdata(2:end,1),Water.textdata(2:end,2));
d1 = datenum(x,'yyyymmm');
ym1=input(' Please input first Year and Month as [Year, Month]: ');
ym2=input(' Please input last Year and Month as [Year, Month]: ');
l1 = datenum([ym1,1;ym2,1]);
out = Water.data(l1(1)>=d1 & l1(2)<=d1);
or
[y,m] = datevec(d1);
l1 = find(cumsum(ismember([y,m], [ym1;ym2],'rows'))==1);
out = Water.data([l1;l1(end)+1]);
  2 个评论
Walter Roberson
Walter Roberson 2015-10-2
Please attach a part of your file melbournewaterlevels.csv

请先登录,再进行评论。

类别

Help CenterFile Exchange 中查找有关 Data Type Conversion 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by