Undefined Variable or Function Fix?
Mostra commenti meno recenti
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 Commenti
Image Analyst
il 27 Apr 2017
Modificato: Image Analyst
il 27 Apr 2017
Nowhere in your code is either Fix or fix mentioned. Attach the text file if you want us to try your code and try to help you. And please post the entire error message ( ALL the red text, not a paraphrased or small snippet from it like you did).
Image Analyst
il 27 Apr 2017
I don't get that error at all. I get
Error using importdata (line 225)
Unable to load file.
Use TEXTSCAN or FREAD for more complex formats.
Error in test1 (line 20)
A = importdata(fname_in,'%f%f%f%f%f', ' ',1);
Caused by:
Error using textscan
Name-value pair arguments must come in pairs.
Final_Data.txt is this:
inlet
0, -0.25, 2
exit
0, 400, 0.5
fitting
10, 1, 4
pipe
5, 2, 0.00015, 0, 1
pipe
10012.5, 0.5, 0.01, 500, 2
pipe
1005, 3, 0.01, 400, 3
pump
800, 4
turbine
-150, 5
With strings interleaved with lines of a varying numbers of numbers. Like the actual error message said, you're going to have to do some other kind of reader for it. I'd probably write my own. Can you do that? Do you know that the numbers for each category are constant? Like inlet will always have 3 numbers after it and pump will always have 2 numbers after it? Or can they vary?
Neil Solan
il 27 Apr 2017
Jan
il 27 Apr 2017
@Neil: Please post the complete error message. It will reveal immediately the location of the problem.
Neil Solan
il 27 Apr 2017
Modificato: Neil Solan
il 27 Apr 2017
Risposte (1)
Star Strider
il 27 Apr 2017
Use single quotes around the file name:
FileName = 'Final_Data.txt';
PIPE_FLOW(FileName,M);
Categorie
Scopri di più su Data Type Conversion in Centro assistenza e File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!