8 views (last 30 days)

Show older comments

Jonathon Klepatzki on 7 Nov 2023

Commented: Mathieu NOE on 15 Nov 2023

Accepted Answer: Mathieu NOE

- windrose.pdf
- WindRose.txt

I am trying to calculate the frequency of the wind direction and speed at specific angles to be used in a compass plot. I am receving an error that states "incorrect inputs or outputs when using the find function. I have attached the PDF of the code and the datafile to be used. Any help would be greatly appreciated.

##### 0 Comments Show -2 older commentsHide -2 older comments

Show -2 older commentsHide -2 older comments

Sign in to comment.

Sign in to answer this question.

### Accepted Answer

Mathieu NOE on 8 Nov 2023

Edited: Mathieu NOE on 8 Nov 2023

Open in MATLAB Online

hello

well, your code looks a bit strange to me

first error is that find does not operate on table elements. You could have loaded directly your data as numeric data only with readmatrix (not need for readtable here)

then there are some lines wher the syntax makes me wonder where this code comes from :

% calculate specific direction based on frequency

N_freq = meanfreq(WNDIR,N_WND_IDX);

W_freq = meanfreq(WNDIR,W_WND_IDX);

S_freq = meanfreq(WNDIR,S_WND_IDX);

E_freq = meanfreq(WNDIR,E_WND_IDX);

I am not aware of a meanfreq function that has 2 input arguments (??)

also

U =

sum(W_freq(S_WSPD_freq,M_WSPD_freq,W_WSPD_freq,WW_WSPD_freq,C_WSPD_freq),E_freq...

(S_WSPD_freq,M_WSPD_freq,W_WSPD_freq,WW_WSPD_freq,C_WSPD_freq))

V =

sum(N_freq(S_WSPD_freq,M_WSPD_freq,W_WSPD_freq,WW_WSPD_freq,C_WSPD_freq),S_freq...

(S_WSPD_freq,M_WSPD_freq,W_WSPD_freq,WW_WSPD_freq,C_WSPD_freq))

wonder what this is supposed to do ... what are you trying to do ? what is the math behind this ?

at the end I wonder why you need to reinvent the wheel as they are already good stuff available on the FEX for wind rose plotting

let's pick this one :

wind_rose(wind_direction,wind_speed) - File Exchange - MATLAB Central (mathworks.com)

and this is the result obtained in less than 1 minute of work

Tbl = readmatrix('WindRose.txt');

WNDIR = Tbl(:,6); % pulling wind direction data from column 6

WSPD = Tbl(:,7); % pulling wind speed data from column 7

wind_rose(WNDIR,WSPD); % see function below

%%%%%% FEX : https://fr.mathworks.com/matlabcentral/fileexchange/65174-wind_rose-wind_direction-wind_speed

function wind_rose(wind_direction,wind_speed)

%WIND_ROSE Plot a wind rose

% this plots a wind rose

figure

pax = polaraxes;

polarhistogram(deg2rad(wind_direction(wind_speed<25)),deg2rad(0:10:360),'displayname','20 - 25 m/s')

hold on

polarhistogram(deg2rad(wind_direction(wind_speed<20)),deg2rad(0:10:360),'FaceColor','red','displayname','15 - 20 m/s')

polarhistogram(deg2rad(wind_direction(wind_speed<15)),deg2rad(0:10:360),'FaceColor','yellow','displayname','10 - 15 m/s')

polarhistogram(deg2rad(wind_direction(wind_speed<10)),deg2rad(0:10:360),'FaceColor','green','displayname','5 - 10 m/s')

polarhistogram(deg2rad(wind_direction(wind_speed<5)),deg2rad(0:10:360),'FaceColor','blue','displayname','0 - 5 m/s')

pax.ThetaDir = 'clockwise';

pax.ThetaZeroLocation = 'top';

legend('Show')

title('Wind Rose')

end

##### 8 Comments Show 6 older commentsHide 6 older comments

Show 6 older commentsHide 6 older comments

Jonathon Klepatzki on 8 Nov 2023

#### Direct link to this comment

https://support.mathworks.com/matlabcentral/answers/2044237-calculating-the-directional-and-magnitude-frequency-of-wind-at-specific-angles#comment_2953687

Edited: Jonathon Klepatzki on 8 Nov 2023

Hey Matt,

I appreciate the help. I am 'relatively new' to coding despite taking one class during my undergrad. Therefore, I spend time writing and figuring out what works and doesn't work while developing something.

The attempt with frequency is to find how often specific wind directions occur (work demands). Any suggestions?

I also need to extract time from the file (datetime, I think would be good), any suggestions?

I am trying to do one thing. How can we extract data from a specific set of rows and plot it using wind rose? For example, use data from row 2751:2798). I tried it just now and got an error ("index must not exceed 18 or index in position 2 exceeds array bounds")

(Don't ask about the sum code, it was an experimental attempt).

I also appreciate the help with the code you provided as I found the wind rose function and couldn't quite get it to work.

Mathieu NOE on 8 Nov 2023

#### Direct link to this comment

https://support.mathworks.com/matlabcentral/answers/2044237-calculating-the-directional-and-magnitude-frequency-of-wind-at-specific-angles#comment_2953752

hello again

ok, I understand you wanted to create your own code and not rely on someone else code (including the wind rose function)

now this is a work that is maybe a bit tough for someone just starting coding so I wonder what kind of result you need to provide

IMO what you have to do is to create a histogram , but here it would be nice to have it in polar coordinates and that's exactly what polarhistogram can provide

so I wonder why someone needs to reinvent this function or if you are allowed to use it

Mathieu NOE on 8 Nov 2023

#### Direct link to this comment

https://support.mathworks.com/matlabcentral/answers/2044237-calculating-the-directional-and-magnitude-frequency-of-wind-at-specific-angles#comment_2953767

also I still don't have the answer about where does your meanfreq function comes from ? seems not in the standard matlab package (the only one I am aware comes from the Signal Processing Toolbox and has only one input argument : Mean frequency - MATLAB meanfreq - MathWorks France

Jonathon Klepatzki on 8 Nov 2023

#### Direct link to this comment

https://support.mathworks.com/matlabcentral/answers/2044237-calculating-the-directional-and-magnitude-frequency-of-wind-at-specific-angles#comment_2953792

Edited: Jonathon Klepatzki on 8 Nov 2023

Yeah, it's a challenge for sure. I'm actually being forced to learn Python as I go (no excuse is allowed here at my particular position).

Before I found the wind rose code, is why I tried to develop my own. However, if I can find someone else's code that does what I need, I will gladly use it.

Ultimately, what I need is the number of times and count of (frequency) specific wind directions occurred (e.g., 330 degrees occurred x amount of times at time x on the 1st of January) at a given sector. Do that for the entire 24-hr period of January 1st. The follow-up will then do that for every single day and plot each one.

Jonathon Klepatzki on 13 Nov 2023

#### Direct link to this comment

https://support.mathworks.com/matlabcentral/answers/2044237-calculating-the-directional-and-magnitude-frequency-of-wind-at-specific-angles#comment_2959702

@Mathieu NOE,

Can you verify my results from this code? I rewrote the main body to the following:

x = readmatrix('WindRose.txt')

WNDIR = x(2:2798,6);

WSPD = x(2:2798,7);

[C,ia] = unique(WNDIR)

W = WNDIR(ia,:)

[S,ia1] = unique(WSPD)

M = WSPD(ia1,:)

wind_rose(W,M)

I'm hoping that the unique function allows me to plot the windrose given the number of times a specific DIR and magnitude occurs beginning with the first row (row 2). On my end, the code works. I just would like a professional overview. Thanks!

Mathieu NOE on 14 Nov 2023

#### Direct link to this comment

https://support.mathworks.com/matlabcentral/answers/2044237-calculating-the-directional-and-magnitude-frequency-of-wind-at-specific-angles#comment_2960377

Open in MATLAB Online

hello again

a few suggestions to improve your code (don't forget also the ; at the end of each line , otherwise you fill your command window with the content of each variable that appears in the line)

I wonder if applying unique is a good thing , why only plot unique events ? if the wind blows often at the same speed and / or direction, why hide this is the plot ?

x = readmatrix('WindRose.txt');

ind = (2:2798); % create once the rows indice and use it as many times as needed (reduce risk of bad manual input)

WNDIR = x(ind,6);

WSPD = x(ind,7);

[WNDIR,ia] = unique(WNDIR); % you have alreay the "new" WNDIR in the function output

WSPD = WSPD(ia,:); % don't forget to apply the ia selection to WSPD as well

[WSPD,ia] = unique(WSPD); % you have alreay the "new" WSPD in the function output

WNDIR = WNDIR(ia,:); % don't forget to apply the ia selection to WNDIR as well

wind_rose(WNDIR,WSPD);

Jonathon Klepatzki on 14 Nov 2023

#### Direct link to this comment

https://support.mathworks.com/matlabcentral/answers/2044237-calculating-the-directional-and-magnitude-frequency-of-wind-at-specific-angles#comment_2960952

Thank you for the tip! I was hoping that the unique function would do the following:

- count the number of times a specific wind direction occurred (count) at a specific zone (e.g. 0 or 360 is zone 1)
- calculate the frequency those winds occurred
- plot the wind rose
- disp or print the chart of the results

Mathieu NOE on 15 Nov 2023

#### Direct link to this comment

https://support.mathworks.com/matlabcentral/answers/2044237-calculating-the-directional-and-magnitude-frequency-of-wind-at-specific-angles#comment_2961722

well that's exactly the contrary, unique will remove all duplicates so in terms of statistics your are not taking into account the most frequent events and you will miss that in your plot

what you need to do is an histogram (see histcount) and that's what is doing the wind_rose function with polarhistogram

Sign in to comment.

### More Answers (1)

Sakshi Sharma on 8 Nov 2023

Open in MATLAB Online

find isn't going to work on a table, but it will work on the contents of a table. So in this case you can write:

W_WND_IDX = find(WNDIR.WDIR < 330 & WNDIR.WDIR > 210);

##### 1 Comment Show -1 older commentsHide -1 older comments

Show -1 older commentsHide -1 older comments

Jonathon Klepatzki on 8 Nov 2023

#### Direct link to this comment

https://support.mathworks.com/matlabcentral/answers/2044237-calculating-the-directional-and-magnitude-frequency-of-wind-at-specific-angles#comment_2953692

Hi Sakshi,

That's awesome, I appreciate the help. I gotta figure out how to frequencies now.

Sign in to comment.

Sign in to answer this question.

**An Error Occurred**

Unable to complete the action because of changes made to the page. Reload the page to see its updated state.

Select a Web Site

Choose a web site to get translated content where available and see local events and offers. Based on your location, we recommend that you select: .

You can also select a web site from the following list

Americas

- América Latina (Español)
- Canada (English)
- United States (English)

Europe

- Belgium (English)
- Denmark (English)
- Deutschland (Deutsch)
- España (Español)
- Finland (English)
- France (Français)
- Ireland (English)
- Italia (Italiano)
- Luxembourg (English)

- Netherlands (English)
- Norway (English)
- Österreich (Deutsch)
- Portugal (English)
- Sweden (English)
- Switzerland
- Deutsch
- English
- Français

- United Kingdom(English)

Asia Pacific

- Australia (English)
- India (English)
- New Zealand (English)
- 中国
- 日本Japanese (日本語)
- 한국Korean (한국어)

Contact your local office