How to create a function that produces 2 histograms?

2 Ansichten (letzte 30 Tage)
Jakub
Jakub am 14 Dez. 2022
Kommentiert: Steven Lord am 14 Dez. 2022
I have a dataset with 2 columns and 9862 rows.
I need to make a figure consisting of two panels where the upper panel is the histogram of one column and the lower panel is the histogram of the other column.
Dd = RiverData(:,1); % daily discharge
Dr = out(); % daily rainfall
function histfunction(Dd,Dr)
% histogram function takes in the daily discharge and rainfall and histograms them
% Dd = Daily discharge, Dr = Daily rainfall
plot(Dr,Dd)
end
So far, I've come up with this (I'm only making one right now but I can't even manage that), but clearly I have made some sort of mistake and I can not figure it out.

Antworten (2)

Steven Lord
Steven Lord am 14 Dez. 2022
Use tiledlayout and call histogram twice, once for each tile in the tiled layout.
  2 Kommentare
Jakub
Jakub am 14 Dez. 2022
how would i incorporate this into a function though? i am very new to matlab
Steven Lord
Steven Lord am 14 Dez. 2022
Most functions in MATLAB include examples on their documentation pages. The tiledlayout documentation page to which I linked above has 14 examples and the histogram documentation page has 10.
Part of the purpose of those examples is to teach users how to use those functions, but another part of their purpose is to serve as starting points. Find examples on those pages that look close to what you want to do, copy them into your code, and modify them so they meet your needs. Once you have them working as a script, create a function file and put that code inside it. You'll need to decide what input and output arguments your function should have, but the documentation describes how to specify those in your function's definition statement.

Melden Sie sich an, um zu kommentieren.


Cris LaPierre
Cris LaPierre am 14 Dez. 2022
I'd use tiledlayout. Rather than surf or countour, use histogram.
tiledlayout(2,1);
[X,Y,Z] = peaks(20);
% Tile 1
nexttile
surf(X,Y,Z)
% Tile 2
nexttile
contour(X,Y,Z)

Kategorien

Mehr zu Data Distribution Plots finden Sie in Help Center und File Exchange

Produkte

Community Treasure Hunt

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

Start Hunting!

Translated by