Julia: Tutorial & Code-Collection¶

Source: https://github.com/markomlikota/CodingSoftware

MIT License, © Marko Mlikota, https://markomlikota.github.io

Path-, Folder- & Workspace-Management¶

In [1]:
# Look up present working directory:

pwd()
Out[1]:
"/files/Julia/TutorialCodecollection"
In [2]:
# Set up working directory:

cd("/files/Julia/") # (insert your own path here)
In [3]:
# It is a good idea to store the paths you frequently use:

sPathMain            = "/files/Julia/TutorialCodecollection/" # (insert your own path here)
sPathFunctions       = sPathMain * "Functions/"
sPathOutput          = sPathMain * "/Output/"

cd(sPathMain)

# It's best practice to let all your paths end with a backslash, 
# so that you can reference individual files or folders in them by just adding 
# the corresponding filename.
In [4]:
# List all folders/files in pwd:

readdir()
Out[4]:
19-element Vector{String}:
 ".ipynb_checkpoints"
 "Julia-Tutorial_AllCodes.jl"
 "Julia-Tutorial_ExercisesSolutions.ipynb"
 "Julia-Tutorial_Sec01_Intro.ipynb"
 "Julia-Tutorial_Sec02_BasicsScalars.ipynb"
 "Julia-Tutorial_Sec03_Functions.ipynb"
 "Julia-Tutorial_Sec04_Vectors.ipynb"
 "Julia-Tutorial_Sec05_MatricesArrays.ipynb"
 "Julia-Tutorial_Sec06_ConditionalsLoops.ipynb"
 "Julia-Tutorial_Sec07_ObjectScopes.ipynb"
 "Julia-Tutorial_Sec08_FurtherObjecTypes.ipynb"
 "Julia-Tutorial_Sec09_PathFolderWorkspaceManagement.ipynb"
 "Julia-Tutorial_Sec10_StoringLoadingData.ipynb"
 "Julia-Tutorial_Sec11_RandomVariables.ipynb"
 "Julia-Tutorial_Sec12_Parallelization.ipynb"
 "Julia-Tutorial_Sec13_Plotting.ipynb"
 "Julia-Tutorial_Sec14_Further.ipynb"
 "Julia-Tutorial_Sec15_CodingAdvice.ipynb"
 "temp_Intro+CollectionCodesJulia.jl"
In [5]:
# Create new folder in present working directory:

mkdir("mynewfolder")

# Note: this returns error if folder already exists.
# Hence, best wrap folder-creation in try-catch-environment:
try
    mkdir("mynewfolder")
catch
end

# Note: no need to navigate to the folder where a sub-folder should be created; 
# can also just type the whole path of the desired new (sub-)folder:
sPathMynewfolder          = sPathMain * "/mynewfolder/"
try
    mkdir(sPathMynewfolder)
catch
end
In [6]:
# Remove folder/file in present working directory:
rm("mynewfolder")
In [7]:
# It is a good idea to divide project into several files.
# An external julia-file can then be called from the present script by typing e.g.

include(sPathFunctions * "fMyAmazingFunction.jl") # this gives error, since no such file exists
SystemError: opening file "/files/Julia/TutorialCodecollection/Functions/fMyAmazingFunction.jl": No such file or directory

Stacktrace:
  [1] systemerror(p::String, errno::Int32; extrainfo::Nothing)
    @ Base ./error.jl:176
  [2] systemerror
    @ ./error.jl:175 [inlined]
  [3] open(fname::String; lock::Bool, read::Nothing, write::Nothing, create::Nothing, truncate::Nothing, append::Nothing)
    @ Base ./iostream.jl:293
  [4] open
    @ ./iostream.jl:275 [inlined]
  [5] open(f::Base.var"#433#434"{String}, args::String; kwargs::@Kwargs{})
    @ Base ./io.jl:394
  [6] open
    @ ./io.jl:393 [inlined]
  [7] read
    @ ./io.jl:486 [inlined]
  [8] _include(mapexpr::Function, mod::Module, _path::String)
    @ Base ./loading.jl:2132
  [9] include(fname::String)
    @ Base.MainInclude ./client.jl:489
 [10] top-level scope
    @ In[7]:4

Try Exercises 9.a.i - 9.a.ii