Issue with addpath files

3 次查看(过去 30 天)
Briana Canet
Briana Canet 2023-11-19
I have two files that rely on each other but they aren't working.
File 1
File 2
Code File 1
function u = u1(commute)
a = -0.09428;
b = 0.03194;
c = 1.096;
d = -0.008854;
if commute >= 0 && commute <= 60
u = a*exp(b*commute) + c*exp(d*commute);
else
u = 0;
end
end
function u = u2(cost)
a = -0.006736;
b = 0.001477;
c = 1.389;
d = -0.0002998;
if cost >= 1000 && cost <= 3000
u = a*exp(b*cost) + c*exp(d*cost);
elseif cost < 1000
u = 1;
else
u = 0;
end
end
function u = u3(space)
a = 1.466;
b = -1.941e-5;
c = -9.688;
d = -0.001279;
p1 = -0.001;
p2 = 3.5;
if space <= 2500
u = a*exp(b*space) + c*exp(d*space);
elseif space > 2500 && space <= 3500
u = p1*space + p2;
else
u = 0;
end
end
function u = u4(style)
switch style
case {1,'Midcentury Modern'}
u = 0;
case {2,'Ranch'}
u = 0.2;
case {3,'Contemporary'}
u = 0.3;
case {4,'Cape Cod'}
u = 0.4;
case {5,'Craftsman'}
u = 0.5;
case {6,'Country French'}
u = 0.6;
case {7,'Cottage'}
u = 0.65;
case {8,'Colonial'}
u = 0.85;
case {9,'Tudor'}
u = 0.9;
case {10,'Victorian'}
u = 1.0;
end
end
Code File 2
close all; clc; clear;
addpath('UtilityFunctions');
% First Comparison
S1 = [60 2300 1850 5];
S2 = [25 2700 1650 2];
U1 = [u1(S1(1)) u2(S1(2)) u3(S1(3)) u4(S1(4))];
U2 = [u1(S2(1)) u2(S2(2)) u3(S2(3)) u4(S2(4))];
% Second Comparison
S3 = [35 3000 1850 5];
S4 = [50 1300 1650 2];
U3 = [u1(S3(1)) u2(S3(2)) u3(S3(3)) u4(S3(4))];
U4 = [u1(S4(1)) u2(S4(2)) u3(S4(3)) u4(S4(4))];
% Third Comparison
S5 = [35 2300 1500 5];
S6 = [50 2700 2050 2];
U5 = [u1(S5(1)) u2(S5(2)) u3(S5(3)) u4(S5(4))];
U6 = [u1(S6(1)) u2(S6(2)) u3(S6(3)) u4(S6(4))];
% Combine terms for left-side matrix
A = [1 1 1 1;
U1(1)-U2(1) U1(2)-U2(2) U1(3)-U2(3) U1(4)-U2(4);
U3(1)-U4(1) U3(2)-U4(2) U3(3)-U4(3) U3(4)-U4(4);
U5(1)-U6(1) U5(2)-U6(2) U5(3)-U6(3) U5(4)-U6(4)];
% Right-side matrix
B = [ 1; 0; 0; 0];
% Solve for scaling constants
K = A^-1*B;
fprintf('K1: %1.4f\nK2: %1.4f\nK3: %1.4f\nK4: %1.4f\n',K(1),K(2),K(3),K(4));

回答(1 个)

Walter Roberson
Walter Roberson 2023-11-19
First of all, you should not addpath() an individual .m file: you should addpath() the folder that the individual .m file is inside.
Second of all, you have several functions stored inside the single file UtilityFunctions.m . When you have several functions in the same function file, then anything outside the .m file can only directly call upon the very first function in the function file -- and must use the name of the .m file to do so.
Your UtilityFunctions.m has function f1 as the first function in it. As a special provision of the MATLAB language, the name of the first function in a function file is ignored and the name of the file is substituted. So instead of calling
U1 = [u1(S1(1)) u2(S1(2)) u3(S1(3)) u4(S1(4))];
you could code
U1 = [UtilityFunctions(S1(1)) u2(S1(2)) u3(S1(3)) u4(S1(4))];
but then you would have the problem that u2, u3, u4 are completely inaccessible.
In order for u2, u3, u4 to be accessible outside of UtilityFunctions.m then the first function in the file would have to create handles to those functions and make those handles available to outside of the file.
In short... do not code a bunch of independent utilities in the same file... it is a nuisance to access them. Put them in individual files. If you were to split them into individual .m files and put those files inside a folder named UtilityFunctions then your code would probably start working.
Or you could use the trick that if you create a class with static methods, then once the class has been loaded into memory, the methods can be called without needing to refer to the class.

类别

Help CenterFile Exchange 中查找有关 Search Path 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by