Passing an array defined inside a function out of the function


#1

Dear Julia community,

I’m having what it probably a simple problem but after a lot of googling and trial and error I’m still at the “error” stage so I thought I’d ask for help :slight_smile:

I have a function that fills an array:

function UnscaleY(Factor::Float64, YList::Array{Float64}, b::Array{Float64})
 CarteY=zeros(YList)
 n=size(YList,1)
 for i in 1:n
  CarteY=Factor*(YList[i]*b[1] + YList[i]*b[2] + YList[i]*b[3])
 end
 return CarteY
end

I would like my return array, CarteY, to be accessible outside the scope of the function so that I can do some more operations with it and then save the result to a file. But when I try that, for example:

Test=zeros(CarteY, 1)
AnotherArrayY=[Test CarteY]
writedlm("another array.txt", AnotherArray)

I get a dimensions mismatch error. Test and CarteY should have the same number of rows (for this case, they are both 54x1 column vectors) and I have verified this in the REPL. I’m not sure where my error is.

Any help is greatly appreciated!


#2

Please include full working code and data.
In this case, you did not show how you called the function UnscaleY.

You probably need to assign CarteY = UnscaleY(something)


#3

Hi @dpsanders, here is how I call the function:

TestY=UnscaleY(Factor, YList, b)

#4

Please provide your complete code snippet that we can run including (fake) data.


#5

Sure thing,

Factor=1.0

 LatticeVectors=readdlm("lattice-vectors")
 a=LatticeVectors[1:1,:]
 b=LatticeVectors[2:2,:]
 c=LatticeVectors[3:3,:]
 println("...the lattice vectors are:")
 println("a=", a)
 println("b=", b)
 println("c=", c)

DirectList=readdlm("direct-list")
XList=DirectList[1:54,1]
YList=DirectList[1:54,2]
ZList=DirectList[1:54, 3]


function UnscaleX(Factor::Float64, XList::Array{Float64}, a::Array{Float64})
 CarteX=zeros(XList)
 n=size(XList,1)
 for i in 1:n
  CarteX[i]= Factor*(XList[i]*a[1] + XList[i]*a[2] + XList[i]*a[3])
 end
 CarteX
end

function UnscaleY(Factor::Float64, YList::Array{Float64}, b::Array{Float64})
 CarteY=zeros(YList)
 n=size(YList,1)
 for i in 1:n
  CarteY=Factor*(YList[i]*b[1] + YList[i]*b[2] + YList[i]*b[3])
 end
 return CarteY
end

function UnscaleZ(Factor::Float64, ZList::Array{Float64}, c::Array{Float64})
 CarteZ=zeros(ZList)
 n=size(ZList,1)
 for i in 1:n
  CarteZ=CartesianZList=Factor*(ZList[i]*c[1] + ZList[i]*c[2] + ZList[i]*c[3])
 end
 return CarteZ
end

TestX=zeros(XList)
TestY=zeros(YList)
TestZ=zeros(ZList)
TestX=UnscaleX(Factor, XList, a)
TestY=UnscaleY(Factor, YList, b)
TestY=UnscaleZ(Factor, ZList, c)

Cartesian=zeros(54, 3)
Cartesian=[TestX TestY TestZ]
println(Cartesian)
println("...Cartesian coordinates are being saved to the file: cartesian-coords.txt")
writedlm("cartesian-coords.txt", Cartesian)

Files with fake data:
File lattice-vectors contains the following:

 15.2259346732869041   0.0050373306177292   0.0186555174857414
-7.0913762758806689   13.4745115715249604  0.0182226035629158
 -7.0916724110859466  11.7516324991570080  6.5928931085496609

File direct-list contains:

  0.7482245082990034  0.3534019561877609  0.1812378289473642
  0.6178686864118543  0.1681233715252839  0.0821353813181317
  0.4357333985898882  0.9695266986682127  0.8683503946494281
  0.3242964642812215  0.8076126796414442  0.7941651238854253
  0.1812417570426143  0.7482124708854798  0.3533874975116351
  0.0821432447687275  0.6178660181570326  0.1681074617961152
  0.8683593838356138  0.4357342720578281  0.9695141008383492
  0.7941780484555921  0.3243099013687005  0.8075985077022094
  0.3533875088260943  0.1812464919612361  0.7482169792073099
  0.1681051383648486  0.0821450806675383  0.6178521899197107
  0.9695102304529243  0.8683608555517192  0.4357129975650504
  0.8075933403174808  0.7941779831051727  0.3242669341483143
  0.2518079787588328  0.6466360641850607  0.8187536189884783
  0.3821700051047205  0.8319173244530188  0.9178577649264432
  0.5643066165282937  0.0305157504412312  0.1316440805260589
  0.6757386225074868  0.1924286807211093  0.2058276349695207
  0.8187763498964314  0.2518389094923041  0.6466177058035906
  0.9178731599448806  0.3821930874036329  0.8318978053304958
  0.1316586451011760  0.5643317802194968  0.0304930862752822
  0.2058389019103447  0.6757504398400337  0.1924080097315123
  0.6466207033339431  0.8187848140403626  0.2517927628100531
  0.8318961348815688  0.9178750595770820  0.3821416949918647
  0.0304930169122549  0.1316611152535211  0.5642822311429468
  0.1924063837065296  0.2058395520413121  0.6757204995273713
  0.1858282736115658  0.6553591972564562  0.6322799537724728
  0.6322933931195204  0.1858446243288009  0.6553487757883374
  0.6553404514988551  0.6322932698972672  0.1857950510331017
  0.8141950235177683  0.3446830202774450  0.3677091014705928
  0.3677211439898948  0.8142100503972600  0.3446597515296888
  0.3446593118364114  0.3677221543508475  0.8141866785824847
  0.6981865926942720  0.3333917203738143  0.0692480301421015
  0.9043833218257868  0.5264829859982945  0.3701530491344796
  0.3662307749504640  0.9306573408865069  0.7366377267545515
  0.0692521847562757  0.6981793081050421  0.3333827932116585
  0.3701539145218680  0.9043621240139327  0.5264629248805210
  0.7366440236451102  0.3662253009791751  0.9306487282664943
  0.3333867003273195  0.0692587353055780  0.6981855480007444
  0.5264653972743839  0.3701612556937298  0.9043783722804863
  0.9306492271069859  0.7366501298752793  0.3662224046151440
  0.3018462195926945  0.6666441173467359  0.9307425348348507
  0.0956399187615062  0.4735534469652757  0.6298350111775736
  0.6338065183530964  0.0693844350988777  0.2633559683332034
  0.9307653118267606  0.3018601266864653  0.6666226325414922
  0.6298659855286579  0.0956917531533225  0.4735391504587828
  0.2633719239128310  0.6338446853007450  0.0693546518174230
  0.6666289119963787  0.9307756483485854  0.3018102551604652
  0.4735433006407601  0.6298785805458186  0.0956584473233547
  0.0693631109866678  0.2633795254480514  0.6337875662267405
  0.5197348673738973  0.1144740833773222  0.7873118502351895
  0.7873126286326132  0.5197330899686363  0.1144702234295533
  0.1144777633769872  0.7873265496067330  0.5197331478490889
  0.4802955162322206  0.8855815579437910  0.2126891024534877
  0.2127046167967178  0.4803123212502428  0.8855431458900669
  0.8855577438543734  0.2127220547985894  0.4802804552218186

#6
 for i in 1:n
    CarteY=Factor*(YList[i]*b[1] + YList[i]*b[2] + YList[i]*b[3])
 end

looks weird. You are just overwriting CarteY (with a scalar) in every iteration. Did you want (note the [i] on CarteY:

 for i in 1:n
    CarteY[i]=Factor*(YList[i]*b[1] + YList[i]*b[2] + YList[i]*b[3])
 end

?


#8

Thank you! I’ve fixed that and another problem that I found. I’ll put the solution to that in case someone else has a similar problem:

The writing to file section I had initially just printed a long row vector list to the file. To print in [x y z] format, I’ve changed the last bit of code to this:

writedlm("coords.txt", [TestX TestY TestZ], '\t')