From 276d3855ee98d239268782066f175fb7d72e5bcc Mon Sep 17 00:00:00 2001 From: Leif Andersson <leif.andersson@control.lth.se> Date: Sun, 16 Aug 2020 14:53:01 +0200 Subject: [PATCH] Created module. --- Project.toml | 4 ++ src/ControlCommon.jl | 96 ++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 100 insertions(+) create mode 100644 Project.toml create mode 100644 src/ControlCommon.jl diff --git a/Project.toml b/Project.toml new file mode 100644 index 0000000..5aca64f --- /dev/null +++ b/Project.toml @@ -0,0 +1,4 @@ +name = "ControlCommon" +uuid = "9c98e656-dfbc-11ea-2f36-4b421b45bbac" +authors = ["Leif Andersson <leif.andersson@control.lth.se>"] +version = "0.1.0" diff --git a/src/ControlCommon.jl b/src/ControlCommon.jl new file mode 100644 index 0000000..9e0f40f --- /dev/null +++ b/src/ControlCommon.jl @@ -0,0 +1,96 @@ +module ControlCommon + +export printcsv, JuliaTikz; + +using ControlSystems # ControlSystems v0.5.4 +using Plots +using Printf + +# Set some plot defaults +gr(tickfont=font(12,"Times"),legend=false); + + +function sprintdatarow(r) + join(map(n -> @sprintf("%#11.5G", n), r),';') +end + +function sprintheader(r) + join(map(n -> @sprintf("%11s", n), r),';') +end + +function printcsv(csv::String, data::Array{Any}) + @warn """printcsv(csv::String, data::Array{Any}) is deprecated + Use printcsv(data::Array{Any}, csv="") instead"""; + printcsv(data,csv); +end + +function printcsv(data::Array{Any}, csv="") + global name; + if cmp(csv,"") == 0 + csv = name *".csv"; + end; + open(csv, "w") do f + hh = Array{String}(data[1,:]); + dd = convert(Array{Float64,2},data[2:end,1:end]); + println(f, sprintheader(hh)); + map(r->println(f,sprintdatarow(r)),eachrow(dd)); + end +end + +#@deprecate printcsv(csv::String, data::Array{Any}) printcsv(data::Array{Any}, csv="") + +""" + JuliaTikz(tikzfile,delete=true) + +Extract and run the Julia part of a combined tikz/Julia file. + +The boundary between the two is the string + + \\endinput Julia <name> + +where `<name>` is the name that will be given to the Julia file +(with .jl appended), and also to a variable `name` in the program, +i.e. name = "<name>". + +The Julia file will be extracted to a temporary directory, which will be +deleted after the run unless the optional argument `delete` is false. +""" +function JuliaTikz(tikzfile,delete=true) + dir = mktempdir(); + filespec = ""; out=""; + if !delete + println("Opening " * tikzfile) + end; + open(tikzfile) do file + res = nothing; + while res === nothing + if eof(file) + error("\"\\endinput Julia \" not found"); + end; + line = readline(file); + res = match(r"(\s*\\endinput\s+Julia\s+)([[:alnum:]]+)", line); + end; + + name = res.captures[2]; + filespec = join([dir, name * ".jl"],"/"); + out = open(filespec,"w"); + if !delete + println("Writing " * filespec); + end; + firstline = "name = \"" * name * "\""; + println(out,firstline); + while !eof(file) + println(out,readline(file)); + end; + close(out); + end; + include(filespec); + + if delete + rm(dir,recursive=true); + else + return filespec; + end; +end; + +end; # end module -- GitLab