You just need to call functions for passings input/output into/from functions in main script. After saving each function in separate m files, you can call those function in your main script.
For example,
LagoonInput.m
function [time, height] = LagoonInput
time = importdata('time.txt');
height = importdata('height.txt');
end
LagoonProcess.m
function [h1, Power]=LagoonProcess(time, height)
d = 7.2;
al = 11500;
cd = 0.8;
ct = 0.5;
g = 9.807;
p = 1029;
A = 40.715;
h1 = 14.05;
h2 = height(7988:19295,:);
VFull = 161575;
discharge = zeros(11307, 1);
Power = zeros(11307, 1);
NewVolume = zeros(11307, 1);
h1New = zeros(11307, 1);
for c=1:11307
discharge(c) = cd*A*(sqrt(2*g*((abs(h1-h2(c)))))) ;
V = discharge(c)/A;
Power(c) = (1/2)*ct*p*A*V^3;
NewVolume(c) = VFull - discharge(c);
VFull=NewVolume(c);
h1New(c) = ((VFull - discharge(c))/al);
h1=h1New(c);
end
end
LagoonOutput.m
function LagoonOutput(Power, h1New)
b = Power(:,1:7644);
z = NewTime;
plot(z,b)
xlabel('Time (s)')
ylabel('Power (W)')
title('Time vs Power')
h = h1New(1:7644,:);
y = z;
plot(y,h)
xlabel('Time (s)')
ylabel('Height (m)')
title('Time vs Height')
end
And here is a main script which calls each function.
mainScript.m
[time, height]=LagoonInput;
[h1,Power]=LagoonProcess(time, height);
LagoonOutput(Power, h1New);
Powertotal= sum(Power);
Instead of saving each function as separate files, you can use nested functions. Below is a documentation link.