MethodError: no method matching getindex

Dear All,

I’m trying to call a function(.jl) in a folder. terminal reported a bug:
"MethodError: no method matching getindex"

Attached text you find the test code:


function testc(Nx,Ny,c0)

NxNy = Nx*Ny
ccc=zeros(NxNy,1)
for i=1:Nx
for j=1:Ny
ii =(i-1)*Nx + j*Ny
ccc[ii] =c0 + ii*NxNy
end
end

return  ccc
end
 # main function
 include("testc.jl")
 
 Nx=8
 Ny=12
 c0 = 0.2
 ccc = testc[Nx,Ny,c0]

Thanks in advance!

You are using square brackets testc[Nx, Ny, c0] in calling your function when you should be using round brackets testc(Nx, Ny, c0)

I seem to remember that you are transitioning from Matlab - I think you’ll find with time that this is one of the key day-to-day improvements working in Julia compared to Matlab. Whereas Matlab uses round brackets both for indexing (e.g. my_array(1:5)) and function calls (e.g. my_function(argument)), in Julia square brackets are reserved for indexing and function calls work with round brackets, making the likelihood of introducing bugs by confusing the two a lot lower!

2 Likes

Hi nilshg,

such results came from MATLAB to Julia translator | MATLAB to Julia converter
Matlab:
[out1,out2,…,outn] = functionname(arg1, arg2, …, argn)

Julia:
[out1,out2,…,outn] = functionname[arg1, arg2, …, argn]

Many many thanks for your support!
Best!

I would recommend against using any of this type of automatic conversion tool. As you can see, they don’t really work and in the end it will likely take longer time to use the tool than to do it manually. Also, if you want to program in Julia you still need to learn the language, and rewriting a program in another programming language is a great way to learn.

3 Likes

sure, you are fully correct!!

I agree with @kristoffer.carlsson, although to make life easier for people who still want to use the translator I’ve filed an issue here.

1 Like

The translation is fundamentally impossible because you don’t know if the symbol is a function or an array by just parsing the code.

2 Likes

This was discussed with @Aquaman previously, to apparently little effect.

@Tamas_Papp , I used your suggested web ( tools, eg this one , translate a subset) to test a very very simple function, previously, to apparently little effect.

Please do not misrepresent what I said — as clarified in that topic, I never suggested that you use this tool, or any other.

That said, you are of course free to ignore advice from multiple experienced Julia users, but this case is a perfect demonstration of why translation tools will not help you. You would be much better off just learning Julia.