# Reading data .mat files and merge them

**URL:** https://discourse.julialang.org/t/reading-data-mat-files-and-merge-them/13375
**Category:** General Usage
**Created:** [August 13, 2018, 2:07pm UTC](https://discourse.julialang.org/t/reading-data-mat-files-and-merge-them/13375 "2018-08-13T14:07:01Z")
**Posts on this page:** 9
**Page:** 1

<div class="post-metadata">

### Author: ![jayce\_ram](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jayce_ram/32/3977_2.png) [@jayce\_ram](https://discourse.julialang.org/u/jayce_ram)
#### Post date: [August 13, 2018, 2:07pm UTC](https://discourse.julialang.org/t/reading-data-mat-files-and-merge-them/13375/1 "2018-08-13T14:07:01Z")

</div>

I have multiples files ` .mat` that are organized at the same way. They look like that:

```julia
# file name : Case_1 
# Created by Octave 4.2.0
# matrix
# other comments
1 2 3 
4 5 6

# vector
0.2
0.3
0.4

```

And I’d like to get these values and group them in two other files. Let’s assume the second file it’s like that:

```julia
# file name : Case_2
# Created by Octave 4.2.0
# matrix
# other comments
7 8 9 
10 11 12

# vector
0.5
0.6
0.7

```

So, the final files would look like that :

```julia
# file name :Merge Matrix Case_1 Case_2
# Created by Octave 4.2.0
# matrix
# other comments
1 2 3 7 8 9 
4 5 6 10 11 12

```

AND this other one:

```julia
# file name :Merge Vector Case_1 Case_2
# Created by Octave 4.2.0
# vector
0.2
0.3
0.4
0.5
0.6
0.7

```

Anyone know if it’s really possible to do it in Julia ? Thanks in advance!  
p.s : I have no access to the code source in octave, I only have the output data.

---

<div class="post-metadata">

### Author: ![Tamas\_Papp](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tamas_papp/32/25949_2.png) [@Tamas\_Papp](https://discourse.julialang.org/u/Tamas_Papp)
#### Post date: [August 13, 2018, 2:28pm UTC](https://discourse.julialang.org/t/reading-data-mat-files-and-merge-them/13375/2 "2018-08-13T14:28:05Z")

</div>

```julia
using DelimitedFiles

function quick_and_dirty_read(filename)
    m = readdlm(filename; comments = true, comment_char = '#')
    vector_rows = isempty.(m[:, 2])
    Int.(m[.!vector_rows, :]), Float64.(m[vector_rows, 1]) # modify types or do something else if necessary
end

```

reads the file, do the concatenation, then write it out similarly.

---

<div class="post-metadata">

### Author: ![jayce\_ram](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jayce_ram/32/3977_2.png) [@jayce\_ram](https://discourse.julialang.org/u/jayce_ram)
#### Post date: [August 13, 2018, 3:01pm UTC](https://discourse.julialang.org/t/reading-data-mat-files-and-merge-them/13375/3 "2018-08-13T15:01:29Z")

</div>

Thanks for your answer. Unfortunately, I have tried to install this pack but I got this error :

```julia
julia> Pkg.add("DelimitedFiles")
ERROR: unknown package DelimitedFiles
macro expansion at .\pkg\entry.jl:53 [inlined]
(::Base.Pkg.Entry.##1#3{String,Base.Pkg.Types.VersionSet})() at .\task.jl:335

```

Do you have an idea? I actually use Juno for JuliaPro 0.6.2.2

---

<div class="post-metadata">

### Author: ![j605](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/j605/32/2130_2.png) [@j605](https://discourse.julialang.org/u/j605)
#### Post date: [August 13, 2018, 3:03pm UTC](https://discourse.julialang.org/t/reading-data-mat-files-and-merge-them/13375/4 "2018-08-13T15:03:18Z")

</div>

I am guessing the answer was for 0.7/1.0. For 0.6, you don’t need to import anything for `readdlm` function.

---

<div class="post-metadata">

### Author: ![jayce\_ram](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jayce_ram/32/3977_2.png) [@jayce\_ram](https://discourse.julialang.org/u/jayce_ram)
#### Post date: [August 13, 2018, 3:11pm UTC](https://discourse.julialang.org/t/reading-data-mat-files-and-merge-them/13375/5 "2018-08-13T15:11:25Z")

</div>

that’s true thanks !!

---

<div class="post-metadata">

### Author: ![Tamas\_Papp](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tamas_papp/32/25949_2.png) [@Tamas\_Papp](https://discourse.julialang.org/u/Tamas_Papp)
#### Post date: [August 13, 2018, 3:13pm UTC](https://discourse.julialang.org/t/reading-data-mat-files-and-merge-them/13375/6 "2018-08-13T15:13:25Z")

</div>

Yes, since 1.0 is released I am now assuming that everyone uses that 😉

---

<div class="post-metadata">

### Author: ![jayce\_ram](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jayce_ram/32/3977_2.png) [@jayce\_ram](https://discourse.julialang.org/u/jayce_ram)
#### Post date: [August 14, 2018, 3:29pm UTC](https://discourse.julialang.org/t/reading-data-mat-files-and-merge-them/13375/7 "2018-08-14T15:29:45Z")

</div>

I have tried to change the type of my data, but I got this message:

```julia
ERROR: LoadError: MethodError: no method matching zero(::Type{Any})
Closest candidates are:
  zero(::Type{Union{Missings.Missing, T}}) where T at C:\JuliaPro-0.6.2.2\pkgs-0.6.2.2\v0.6\Missings\src\Missings
.jl:97
  zero(::Type{Base.LibGit2.GitHash}) at libgit2\oid.jl:106
  zero(::Type{Base.Pkg.Resolve.VersionWeights.VWPreBuildItem}) at pkg\resolve\versionweight.jl:82

```

My code is

```julia
for file in list_file
    data = readdlm(file; comments = true, comment_char = '#')
    vector_rows = isempty.(data[:, 2])
    float(data[vector_rows, :])  
end

```

I have also tried by using `convert(Float64,data)` but I got another error:

```julia
ERROR: LoadError: MethodError: Cannot `convert` an object of type Array{Any,2} to an object of type Float64
This may have arisen from a call to the constructor Float64(...),
since type constructors fall back to convert methods.

```

Do you have any idea how I could convert my data? Thanks in advance.

---

<div class="post-metadata">

### Author: ![Tamas\_Papp](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tamas_papp/32/25949_2.png) [@Tamas\_Papp](https://discourse.julialang.org/u/Tamas_Papp)
#### Post date: [August 14, 2018, 3:35pm UTC](https://discourse.julialang.org/t/reading-data-mat-files-and-merge-them/13375/8 "2018-08-14T15:35:30Z")

</div>

For the first error, it would be better to see the code that produced it.

For the second one, try `float.` (not the `.`, for broadcasting).

---

<div class="post-metadata">

### Author: ![jayce\_ram](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jayce_ram/32/3977_2.png) [@jayce\_ram](https://discourse.julialang.org/u/jayce_ram)
#### Post date: [August 14, 2018, 3:56pm UTC](https://discourse.julialang.org/t/reading-data-mat-files-and-merge-them/13375/9 "2018-08-14T15:56:26Z")

</div>

The error `ERROR: LoadError: MethodError: no method matching zero(::Type{Any})` has been produced by the following code, using `float()`:

```julia
clearconsole()
# this code aims to get the values of a .mat file, then save it in dataframe 
# each file represents a period, so the goal is to concanet the files in order to have all the periods into a SAME dataframe 
# in order to respect the standard model, the lignes and columns might be transposed. 
using DataFrames
list_file = readdir() # getting file names
key = "1" 
nb_hours = 24  
nb_points = 23 

list_file_real = [file for file in list_file if contains(file,key)] # excluding unnecessary files
periods= ["1-1_31-3","1-4_30-6","1-7_30-9","1-10_31-12"]
# Getting the real order
# WORKAROUND https://stackoverflow.com/questions/45013963/julia-sort-string-array-by-substring-containment
sort!(list_file_real,by=t->first(filter(x->contains(t,x[2]),enumerate([periods;""])))[1]) 
# the information contained in the following lines and columns:[nb_hours+1:nb_hours + nb_points,1] don't need to be repeted so
# they can placed OUT of the loop
file = list_file_real[1]
data_temp = readdlm(file; comments = true, comment_char = '#')
vector_final_case1_ProbPower = data_temp[nb_hours+1:nb_hours + nb_points,1]

# list of arrays that will keep the values of each file 
matrix_test_case1 = []
vector_average_test_case1 =[]

matrix_test_case2 = [] 
vector_average_test_case2 = []

for file in list_file_real
    data = readdlm(file; comments = true, comment_char = '#')
    vector_rows = isempty.(data[:, 2])
    float(data[vector_rows, :]) 
    # case 1 data 
    probabilities_case1_data =transpose(data[1:nb_hours,1:nb_points]) 
    average_power_case1_data = data[nb_hours + nb_points+1:nb_hours + nb_points+nb_hours,1]

    push!(matrix_test_case1, probabilities_case1_data)
    push!(vector_average_test_case1,average_power_case1_data)
    # case 2 data 
    probabilities_case2_data = data[nb_hours + nb_points+nb_hours + 1:nb_hours + nb_points+nb_hours+nb_hours,1:nb_points] 
    average_power_case2_data = data[nb_hours + nb_points+nb_hours+nb_hours+nb_points+1:nb_hours + nb_points+nb_hours+nb_hours+nb_points+nb_hours,1]
    # 
    push!(matrix_test_case2, probabilities_case2_data)
    push!(vector_average_test_case2, average_power_case2_data)

end

```
