Scaling the X and Y axis of a matlab histogram.

17 vues (au cours des 30 derniers jours)
Tomislav
Tomislav le 24 Juin 2023
Modifié(e) : dpb le 25 Juin 2023
Hello everyone. I downloaded a data set of tidal current velocity and position data for a month (more like 31,5 days, but close to a month) timespan from the NOAA site for a certain location from this link. The data set is in .csv format. I named the file data.csv and uploaded it to my MATLAB Drive (I use MATLAB online, R2023a). The file contains three columns of data: time, velocity and direction.
I successfully read the file and "broke" it into three data arrays by using the following code:
file = readtable("slprj/data.csv", 'PreserveVariableNames' , true);
time = file{:, 1};
velocity = file{:, 2};
direction = file{:, 3};
After that I made a velocity histogram:
histogram(velocity)
I get the following histogram:
The Y axis shows me how many times (in this case, 259 times) the velocity had a value between 0 and 10 cm/s (hence why "Bin edges [0 10]). I need to rescale the X and Y axis in a certain way:
Y axis: every time a certain velocity value appears, it accounts for a 6 minute timespan. So for example, 259*6 minutes = 1554 minutes in a month where the tidal current velocity reached a value between 0 and 10 cm/s. I need the value to be in hours. dividing by 60 in this example you get 25,9 hours, which is basically scaling the Y axis of the original histogram data by dividing it by 10.
X axis: I need to turn cm/s into m/s, so dividing by a 100.
I basically need a hours<>meters/second relationship so I can apply the velocities to the power curve of a tidal turbine because Power[kW]*time[h] = energy[kWh].
Keep in mind that I can't rescale the data arrays from the original .csv file.
Thanks in advance to everyone who helps, also keep in mind that this is my first time asking a question here, so constructive criticism about my question format is welcome.
  1 commentaire
dpb
dpb le 24 Juin 2023
Modifié(e) : dpb le 24 Juin 2023
"Keep in mind that I can't rescale the data arrays from the original .csv file."
Why on earth not, pray tell? We convert data into other units all the time. I recall learning that lesson as almost the very first topic taught in the introductory stoichiometry class back lo! those many years ago.

Connectez-vous pour commenter.

Réponse acceptée

dpb
dpb le 24 Juin 2023
Modifié(e) : dpb le 25 Juin 2023
",,, read the file and "broke" it into three data arrays by using the following code"
file = readtable("slprj/data.csv", 'PreserveVariableNames' , true);
time = file{:, 1};
velocity = file{:, 2};
direction = file{:, 3};
Don't do that---you have the data in a very usable form as a MATLAB table; use it. The variable names are rather cumbersome, though, so let's fix them to be more convenient to use...
tC=readtable("slprj/data.csv"); % it's not a file, it's the current (C) data in table
tC.Properties.VariableNames=extractBefore(tC.Properties.VariableNames,'_'); % Date,Speed,Dir
M=ceil(max(tC.Speed)/10)*10; % max for binning in cm/s units
e=[0:10:M]; % bin edges for histogram
tC.Speed=tC.Speed/100; % convert cm/s to m/s
e=e/100; % and bins to match
hH=histogram(tC.Speed) % histogram in m/s on x, save handle
hAx=gca; % get axes handle
hAx.YTickLabel=hAx.YTick/10; % scale Y axis to hours
xlabel('Tidal Current Speed, m/s')
ylabel('Accumulative Monthly Hours')
hrsMonthly=hH.Values/10; % retrieve bin counts in scaled hours
ADDENDUM:
The other way to approach is to not use histogram on the raw data, but discretize to get the bin counts, then scale those to be the hour sums. Then use bar with those scaled values and the velocity bins in m/s. That way, you don't ever have the issue of trying to call the counts something else (hours). It all works out identically numerically, but may seem somewhat "cleaner" conceptually.
ADDENDUM SECOND:
A final cleaner version that converts the count data to the accumulated hour value per the follow-on discussion would be
...
hH=histogram(tC.Speed) % histogram in m/s on x, save handle
hH.BinCounts=hH.BinCounts/10; % scale to hours/month
xlabel('Tidal Current Speed, m/s')
ylabel('Accumulative Monthly Hours')
hrsMonthly=hH.BinCounts; % retrieve bin counts in scaled hours
  4 commentaires
Tomislav
Tomislav le 25 Juin 2023
Thank you very much! I accepted your original answer because I don't see the option of accepting a reply. I hope if someone is stuck with the same "niche" problem as I am, that they will also read the replies...
dpb
dpb le 25 Juin 2023
It's pretty-much practice in forum to leave conversations as they evolve rather than edit...often the journey is at least as informative as the end if not more so...
I'm such an old fogey I've used the new histogram only in response to queries here so I don't know it well at all...hence initially I wasn't sure it would accept anything other than integer bin counts....and, was indeed also at least influenced by your initial request to not change the results; hence I didn't modify the actual count data, just showed it scaled initially.

Connectez-vous pour commenter.

Plus de réponses (0)

Catégories

En savoir plus sur 2-D and 3-D Plots dans Help Center et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by