Hi,
Simple question I am sure someone can answer. Looked through the help and couldn’t see an obvious example.
##Define these two functions in the REPL:
function TestFunc(X,Pa)
#TestFunc = Moves a point by movement set in Pa
#X - Point to move
#Pa - Movement
for i=1:length(X)
#Centre object 'XYZ' relative to point Pa
X[i] = X[i]-Pa;
end
return(X)
end
function TestFunc2(X,Pa)
#TestFunc2 = Moves a point by movement set in Pa
#X - Point to move
#Pa - Movement
if length(X)==1 #Catch error if X is not an array
X = X-Pa;
else
for i=1:length(X) #And if it is an array do for every point
#Centre object 'XYZ' relative to point PaPbPc
X[i] = X[i]-Pa;
end
end
return(X)
##Now define inputs
X=1.0;
Pa=-1.0;
TestFunc(X,Pa);
TestFunc2(X,Pa);
##If I use:
X=[1.0 1.0];
Pa=-1.0;
TestFunc(X,Pa);
TestFunc2(X,Pa);
##It works fine
For TestFunc we get:
ERROR: MethodError: no method matching setindex!(::Float64, ::Float64, ::Int64)
Stacktrace:
[1] TestFunc(::Float64, ::Float64) at C:\Users\timmm.julia\MyPackage\src\TestFunc.jl:8
[2] top-level scope at none:0
Do I need to include the catch I have added to TestFunc2 if I want to allow my functions to deal with single arrays and vectors? Is this the best way of doing it?
Cheers,
Tim