addpath("C:\Users\")

Dear All,

how can we include files from a folder : addpath("C:\Users") in Julia 1.1.0. As far as I know about include(“abc.jl”)

Is this something you are looking for?

cd(raw"C:\Users") do
    foreach(include, filter!(x -> isfile(x) && endswith(x, ".jl"), readdir()))
end
2 Likes

Hi bkamins,

do we have other choices in Julia (e.g. less commands?)?

Thanks!:slight_smile:

You can define a function that does that and then call it. Note, there’s no need to cd into a directory to read it’s contents:

function include_all_files(dir::String)
    files = filter!(x -> isfile(x) && endswith(x, ".jl"), readdir(dir)
    foreach(include, files)
end

Why do you want to include all the files in a directory in the first place?

2 Likes

I want to call many functions (.jl files) in Julia from the same folder, addpath(‘\sdada’) perhaps is the simplest way? or?

You have to change the directory because readdir returns names relative to the passed directory (so include later will not work if you are not in the right directory).

1 Like

As I know you’re transitioning from MATLAB I thought I’d might be good to point out that in Julia you don’t need to define functions in separate files or by one (I recall doing this with m files circa 2014). So if you’re looking for this to replicate a MATLAB style codebase with lots of tiny files containing function definitions, don’t!

1 Like

Sure,if I want to use e.g. The MATLAB tensor toolbox (https://www.sandia.gov/~tgkolda/TensorToolbox/index-1.0.html) in Julia, I have no idea how to use it directly in Julia, therefore, I have to translate it from matlab to julia, file by file, line by line, …

What I was trying to say was you don’t have to do it like by line necessarily, but can just define all the functions in one file and include that (rather than having to include all files in a folder). I seem to remember that MATLAB has weird restrictions on how functions need to be defined so wanted to point out that Julia doesn’t have these.

I haven’t looked at what you’re trying to translate though, so wouldn’t want to comment on what the best way to organise the code is.

4 Likes