How to output/send data to another function to be used in a calculation
8 views (last 30 days)
Show older comments
How do i send imported data to Function 2 from function 1 and then send the calculated data in function 2 to function 3 to be used to plot a graph. So basically im trying to link all the function so the work together.
%Function 1
function [time, height]=LagoonInput(Name,birthday)
%Importing Data
Time = importdata('time.txt');
%Importing Data
Height = importdata('height.txt');
plot(Height,Time)
% Function 2
function [h1,Power]=LagoonProcess(time,height )
%constants
d = 7.2;
al = 11500;
cd = 0.8;
ct = 0.5;
g = 9.807;
p = 1029;
%Area Of turbine = pi*r^2
A = 40.715;
%Variables that need to change after every loop
h1 = 14.05; %h1 should be replaced with h1New after every 1 loop
h2 = Height(7988:19295,:); %this data is from a data excel sheet
VFull = 161575; %VFull should be replaced by NewVolume after every 1 loop
for c=1:11307 %loop needs to be repeated 16759 times
%Discharge
discharge(c) = cd*A*(sqrt(2*g*((abs(h1-h2(c)))))) ;
V = discharge(c)/A;
%Power
Power(c) = (1/2)*ct*p*A*V^3;
%New Volume Calculation
NewVolume(c) = VFull - discharge(c);
VFull=NewVolume(c);
%New h1
h1New(c) = ((VFull - discharge(c))/al);
h1=h1New(c);
end
function []=LagoonProcess(Power,h1New)
%Function 3
%Power Plot
b = Power(:,1:7644)
z = NewTime;
plot(z,b)
xlabel('Time (s)')
ylabel('Power (W)')
title('Time vs Power')
% Height Plot
h = h1New(1:7644,:)
y = z;
plot(y,h)
xlabel('Time (s)')
ylabel('Height (m)')
title('Time vs Height')
Powertotal= sum(Power)
Answers (1)
Kojiro Saito
on 16 Dec 2019
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
% I'm ommitting input arguments Name and birthday because they're not used
%Importing Data
time = importdata('time.txt');
%Importing Data
height = importdata('height.txt');
end
LagoonProcess.m
function [h1, Power]=LagoonProcess(time, height)
%constants
d = 7.2;
al = 11500;
cd = 0.8;
ct = 0.5;
g = 9.807;
p = 1029;
%Area Of turbine = pi*r^2
A = 40.715;
%Variables that need to change after every loop
h1 = 14.05; %h1 should be replaced with h1New after every 1 loop
h2 = height(7988:19295,:); %this data is from a data excel sheet
VFull = 161575; %VFull should be replaced by NewVolume after every 1 loop
% Adding pre allocates for better performance
discharge = zeros(11307, 1);
Power = zeros(11307, 1);
NewVolume = zeros(11307, 1);
h1New = zeros(11307, 1);
for c=1:11307 %loop needs to be repeated 16759 times
%Discharge
discharge(c) = cd*A*(sqrt(2*g*((abs(h1-h2(c)))))) ;
V = discharge(c)/A;
%Power
Power(c) = (1/2)*ct*p*A*V^3;
%New Volume Calculation
NewVolume(c) = VFull - discharge(c);
VFull=NewVolume(c);
%New h1
h1New(c) = ((VFull - discharge(c))/al);
h1=h1New(c);
end
end
LagoonOutput.m
function LagoonOutput(Power, h1New)
%Function 3
%Power Plot
b = Power(:,1:7644);
z = NewTime;
plot(z,b)
xlabel('Time (s)')
ylabel('Power (W)')
title('Time vs Power')
% Height Plot
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.
0 Comments
See Also
Products
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!