필터 지우기
필터 지우기

Converting to array index

조회 수: 2 (최근 30일)
Steven
Steven 2015년 9월 30일
편집: Andrei Bobrov 2015년 10월 2일
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
Steven
Steven 2015년 10월 2일
Is this of use.

댓글을 달려면 로그인하십시오.

카테고리

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