# Calculate the strain and stress from CSV file

**URL:** <https://discourse.julialang.org/t/calculate-the-strain-and-stress-from-csv-file/94711>\
**Category:** New to Julia\
**Tags:** question\
**Created:** [February 16, 2023, 5:45am UTC](https://discourse.julialang.org/t/calculate-the-strain-and-stress-from-csv-file/94711 "2023-02-16T05:45:01Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Hana](https://avatars.discourse-cdn.com/v4/letter/h/b19c9b/32.png) [@Hana](https://discourse.julialang.org/u/Hana)\
**Post date:** [February 16, 2023, 5:45am UTC](https://discourse.julialang.org/t/calculate-the-strain-and-stress-from-csv-file/94711/1 "2023-02-16T05:45:01Z")

</div>

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?

```julia
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
```

---

<div class="post-metadata">

**Author:** ![uniment](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/uniment/32/24532_2.png) [@uniment](https://discourse.julialang.org/u/uniment)\
**Post date:** [February 16, 2023, 8:03am UTC](https://discourse.julialang.org/t/calculate-the-strain-and-stress-from-csv-file/94711/2 "2023-02-16T08:03:34Z")

</div>

Please read: [Please read: make it easier to help you](https://discourse.julialang.org/t/please-read-make-it-easier-to-help-you/14757)

---

<div class="post-metadata">

**Author:** ![nilshg](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/nilshg/32/2283_2.png) [@nilshg](https://discourse.julialang.org/u/nilshg)\
**Post date:** [February 16, 2023, 10:20am UTC](https://discourse.julialang.org/t/calculate-the-strain-and-stress-from-csv-file/94711/3 "2023-02-16T10:20:38Z")

</div>

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

```julia

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

```julia
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:

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

```

should probably be

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

```

---

<div class="post-metadata">

**Author:** ![nilshg](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/nilshg/32/2283_2.png) [@nilshg](https://discourse.julialang.org/u/nilshg)\
**Post date:** [February 16, 2023, 10:22am UTC](https://discourse.julialang.org/t/calculate-the-strain-and-stress-from-csv-file/94711/4 "2023-02-16T10:22:02Z")

</div>

I would recommend you read

[https://docs.julialang.org/en/v1/manual/functions/](https://docs.julialang.org/en/v1/manual/functions/)

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