Calculate the strain and stress from CSV file

I have CSV file named data.csv. I need to use the 4th column for my calculations. I built the code below but it doesn’t show the final results " strain’ and "stress’'. where can be the error?

using CSV
using Tables

function read_data_file(filename::String)
    CSV.File(filename; header=false, skipto=6) |> Tables.matrix
end

function parse_width_and_thickness(filename::String)
    l::String = ""
    for (i, line) in enumerate(eachline(filename))
        l = line
        if i == 3
          break
        end
   end
   first_split_string = split(l, ",")
   width = parse(Float64, strip(split(first_split_string[3], "=")[2], '"'))
   thickness = parse(Float64, split(split(first_split_string[4], "=")[2], '"')[1])
   (width, thickness)
end

datafile = realpath(dirname(@__FILE__)*"/../data/data.csv")

function convert_to_true_stress_and_strain(filename::String)
  A = broadcast(*, width, thickness)
  
  P = CSV.File("data.csv"; select=[4])  
  ϭE = broadcast(/, P, A)
  strain = ln(1 + ϭE)
  stress = ϭE * ( 1 + ϭE)
  (strain, stress)
end

> Blockquote

Please read: Please read: make it easier to help you

In particular it is helpful to format your code by using triple backticks ```. Here’s what that looks like:


using CSV, Tables

function read_data_file(filename::String)
    CSV.File(filename; header=false, skipto=6) |> Tables.matrix
end

function parse_width_and_thickness(filename::String)
    l::String = “”
    for (i, line) in enumerate(eachline(filename))
        l = line
        if i == 3
            break
        end
    end
    first_split_string = split(l, “,”)
    width = parse(Float64, strip(split(first_split_string[3], "=")[2], '"'))
    thickness = parse(Float64, split(split(first_split_string[4], “=”)[2], '"')[1])
    (width, thickness)
end

datafile = realpath(dirname(@FILE)*"/…/data/data.csv")

function convert_to_true_stress_and_strain(filename::String)
    A = broadcast(*, width, thickness)
    P = CSV.File("data.csv"; select=[4])
    ϭE = broadcast(/, P, A)
    strain = ln(1 + ϭE)
    stress = ϭE * ( 1 + ϭE)
    (strain, stress)
end

When put this way it’s a bit easier to see what’s going wrong - your code snippet just defines three functions (read_data_file, parse_width_and_thickness, and convert_to_true_stress_and_strain), but doesn’t actually call any of the functions. If you want to get values for strain and stress (presumably from your convert_to_true_stress_and_strain function), you need to actually call it like

convert_to_true_stress_and_strain("path_to_my_file")

More generally you seem to be a little confused about how functions and variables work, take the convert_to_true_stress_and_strain for example:

  • You define it to accept a single argument filename, which isn’t actually used anywhere in the function body
  • The first line A = broadcast(*, width, thickness) uses two variables (width and thickness) which aren’t defined anywhere in the function body
  • You then load in some data as P = CSV.File("data.csv"; select=[4]) - this is probably where you actually want to use the filename argument (otherwise the behaviour of the function will depend on which directory you are in when you call it)

Note also that broadcast is almost never used in everday user code, where people reach for the broadcast operator . instead:

    A = broadcast(*, width, thickness)
    ϭE = broadcast(/, P, A)

should probably be

    A = width .* thickness
    ϭE = P ./ A

I would recommend you read

https://docs.julialang.org/en/v1/manual/functions/

to get an idea of how functions and scoping work in Julia.