How to do a Loop statement from cell to double?

3 views (last 30 days)
Hi there
I have a file with wind information (direction and speed), the wind direction has this format (E, N, NE,...) I would like to do a wind rose from 1989 to 2019, so I have a file.xls with multiple sheets (from 1989 to 2019). I'm trying to convert the wind direction from (E, N, NE to 90, 0 , 45). But I do not know how to do it, any help? I post the file (1989.xlsx) and what I've been doing.
%% I created a artificial data to organized wind speed and direction
t1 = datetime(1989,01,01,00,00,00);
t2 = datetime(2019,12,31,23,00,00);
t = (t1:hours(1):t2)';
% my idea is to convert ('E', 'N'... to 90, 0) to the 30 years.
for i = length(1989.VarName1(10:40))
if 1989.Dir{10:40},'E' == 1;
1989.Dir(10:40)= 90;
end
end

Accepted Answer

dpb
dpb on 23 Jan 2021
Use ordinal categorical variables --
t1989=readtable('1989.xlsx','Range','10:40','ReadVariableNames',0); % get in a set of data to play with
tTmp=t1989(1:10,1:3); % just keep 10 rows of the first direction column for illustration
tTmp.Properties.VariableNames={'Dia','Dir','Vel'}; % assign meaningful variable names
This gives us the following table for starters; you'll obviously work over the whole table and all months. There's a problem with the spreadsheet organization for reading default variable names since are duplicated for each of the twelve months; but that is just some bookkeeping to fix up however you choose to to so.
tTmp =
10×3 table
Dia Dir Vel
___ ___ ______
1 E 5.2083
2 SE 7.1667
3 ESE 6.3333
4 ENE 5.9167
5 NE 8.25
6 NNE 9.75
7 NW 10.667
8 N 11.292
9 NW 9.1667
10 N 4.9583
>>
OK, now to the meat of the problem...first convert the direction to a categorical ordinal variable in the defined sequence clockwise from N
t1989.Var2=categorical(t1989.Var2,{'N';'NNE';'NE';'ENE'; ...
'E';'ESE';'SE';'SSE'; ...
'S';'SSW';'SW';'WSW'; ...
'W';'WNW';'NW';'NNW'}, 'Ordinal',1);
tTmp.Theta=(double(tTmp.Dir)-1)*22.5; % calculate/store wind angular direction from direction
The above leaves us with--
tTmp =
10×4 table
Dia Dir Vel Theta
___ ___ ______ _____
1 E 5.2083 90
2 SE 7.1667 135
3 ESE 6.3333 112.5
4 ENE 5.9167 67.5
5 NE 8.25 45
6 NNE 9.75 22.5
7 NW 10.667 315
8 N 11.292 0
9 NW 9.1667 315
10 N 4.9583 0
>>
where the Theta variable is the wind direction in compass degrees associated with the named direction.
Note that is is mandatory to use the 'ordinal' type to indicate the named list of categories is ordered in the direction given on the conversion.

More Answers (1)

Fabian Moreno
Fabian Moreno on 26 Jan 2021
Thank you a lot, it works perfectly.

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!