Undefined Variable or Function Fix?
1 次查看(过去 30 天)
显示 更早的评论
So I have a very long code that is supposed to pull data from a .txt file (attached) and use it to run the code. When I directly import a specific .txt file, the code runs properly. However, I want to put the code into a function where I can input any .txt file name and it will run based on that file. Whenever I put it in a function, I get the error "Undefined Variable 'name of file' or function 'name of file'. I'll post the code below (it's very long but I believe the mistake is in the top several lines) if that helps, but I can't figure out why it will work in a script but not a function.
function [ V_in, V, V_out ] = PIPE_FLOW( fname_in, units )
if units == 'M'
gamma = 9810;
g = 9.81;
rho = 1000;
nu = 1.12e-6;
elseif units == 'E'
gamma = 62.4;
g = 32.2;
rho = 1.94;
nu = 1.21e-5;
end
format shorteng;
format compact;
A = importdata(fname_in,'%f%f%f%f%f', ' ',1);
n=length(A);
%Preallocate the variable vectors:
P_in = zeros(1,n);
z_in = zeros(1,n);
D_in = zeros(1,n);
P_out = zeros(1,n);
z_out = zeros(1,n);
D_out = zeros(1,n);
K_L = zeros(1,n);
V = zeros(1,n);
L = zeros(1,n);
D = zeros(1,n);
z = zeros(1,n);
rough = zeros(1,n);
H_s_Turb = zeros(1,n);
H_s_Pump = zeros(1,n);
Element_Num_Pipe = zeros(1,n);
Element_Num_Fitting = zeros(1,n);
Element_Num_Turbine = zeros(1,n);
Element_Num_Pump = zeros(1,n);
inlet_i = 0;
fitting_i = 0;
turbine_i = 0;
pipe_i = 0;
pump_i = 0;
for i = 1:2:n-1
line_desc = A{i};
line_data = str2num(A{i+1});
% For each pair of input lines, put data directly into relevant
% variable vector to store the data
% if line_desc = 'inlet', then store variables P_in, z_in and D_in
% if line_desc = 'pipe', store variables in relevant useful locations for
% later use.
switch line_desc
case 'inlet'
inlet_i = inlet_i + 1;
P_in(inlet_i) = line_data(1);
z_in(inlet_i) = line_data(2);
D_in(inlet_i) = line_data(3);
case 'exit'
P_out(inlet_i) = line_data(1);
z_out(inlet_i) = line_data(2);
D_out(inlet_i) = line_data(3);
case 'fitting'
fitting_i = fitting_i + 1;
K_L(fitting_i) = line_data(1);
V(fitting_i) = line_data(2);
Element_Num_Fitting(fitting_i) = line_data(3);
case 'pipe'
pipe_i = pipe_i + 1;
L(pipe_i) = line_data(1);
D(pipe_i) = line_data(2);
rough(pipe_i) = line_data(3);
z(pipe_i) = line_data(4);
Element_Num_Pipe(pipe_i) = line_data(5);
case 'pump'
pump_i = pump_i + 1;
H_s_Pump(pump_i) = line_data(1);
Element_Num_Pump(pump_i) = line_data(2);
case 'turbine'
turbine_i = turbine_i + 1;
H_s_Turb(turbine_i) = line_data(1);
Element_Num_Turbine(turbine_i) = line_data(2);
end
end
%Remove the excess zeros from the end of each variable vector:
P_in = P_in(1);
z_in = z_in(1);
D_in = D_in(1);
P_out = P_out(1);
z_out = z_out(1);
D_out = D_out(1);
K_L = K_L(1:fitting_i);
V = V(1:fitting_i);
Element_Num_Pipe = Element_Num_Pipe(1:pipe_i);
Element_Num_Pump = Element_Num_Pump(1:pump_i);
Element_Num_Turbine = Element_Num_Turbine(1:turbine_i);
Element_Num_Fitting = Element_Num_Fitting(1:fitting_i);
L = L(1:pipe_i);
D = D(1:pipe_i);
z = z(1:pipe_i);
rough = rough(1:pipe_i);
H_s_Turb = H_s_Turb(1:turbine_i);
H_s_Pump = H_s_Pump(1:pump_i);
H_s = H_s_Turb(1) + H_s_Pump(1);
%Reference Variables:
D_1 = D(1);
g = 9.81;
gamma = 9810;
nu = 1.12e-6;
%Guess f:
f_g_1 = 0.025;
%Energy Equation:
Num = (2*g)*((P_out-P_in)/gamma+z_out-z_in-H_s);
Denom_guess = ((D_1./D_in).^4)-((D_1./D_out).^4)-f_g_1.*(L./D).*(D_1^4./D.^4)-K_L.*(D_1^4./D.^4);
Vref = sqrt((Num./Denom_guess));
V_1_guess = Vref(1);
rough_1 = rough(1);
rel_rough = (rough_1/D_1);
%Find a Reynolds Number based on the guessed f:
Re = ((V_1_guess*D_1)/nu);
%Guess f:
if 0 < Re && Re < 2100;
f_1_actual = 64/Re;
elseif Re > 4000
f_g_2 = 0.025; %New friction factor guess
while Re > 4000;
f_1_actual = ((-2*log10((rel_rough/3.7)+(2.5/(Re*(sqrt(f_g_2))))))^-2);
if abs(f_g_2 - f_1_actual) < 10^-5;
break
end
f_g_2 = f_1_actual;
end
else
warning('Transitional Flow - Friction Factor cannot be found');
end
Denom_Actual = ((D_1./D_in).^4)-((D_1./D_out).^4)-f_g_1.*(L./D).*(D_1^4./D.^4)-K_L.*(D_1^4./D.^4);
V_final = sqrt((Num./Denom_Actual));
%Graphing Total Head:
H_L = (V_final./D).*(K_L);
Head_Total = H_s_Pump + H_s_Turb + H_L;
plot(Head_Total);
title('Graph of Total Head from inlet to exit');
V_in = V_final(1);
b = length(V_final);
V_out = V_final(b);
Running the code returns this error message:
Undefined variable "Final_Data" or function "Final_Data.txt".
5 个评论
Jan
2017-4-27
@Neil: Please post the complete error message. It will reveal immediately the location of the problem.
回答(1 个)
Star Strider
2017-4-27
Use single quotes around the file name:
FileName = 'Final_Data.txt';
PIPE_FLOW(FileName,M);
0 个评论
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Data Type Conversion 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!