How to store (save and read) multidimensional arrays?

2,187 views
Skip to first unread message

paul analyst

unread,
Mar 30, 2014, 4:47:43 AM3/30/14
to juli...@googlegroups.com
How to store multidimensional arrays?

a = zeros (3,3,3)
for i = 1:27
a [i] = i
end

my array:
1 4 7
2 5 8
3 6 9

11 13 16
11 14 17
...
21 24 27

writecsv ("a.txt" a)
a1 = readcsv ("a.txt")
a2 = reshape (a1, 3,3,3)
a2:
1 10 19
2 11 20
3 12 21

4 13 22
5 14 23
...
9 18 27
a and a2 are different ...
How to store (save and read) multidimensional arrays?
Paul

Tim Holy

unread,
Mar 30, 2014, 6:37:46 AM3/30/14
to juli...@googlegroups.com
If it doesn't have to be a CSV file, try
using HDF5, JLD
@save "a.jld" a

See the docs for HDF5 & JLD.

--Tim

Luis Benet

unread,
Apr 3, 2014, 10:34:22 PM4/3/14
to juli...@googlegroups.com
Hi Paul,

The following is one possibility:


a = zeros (3,3,3)
for i = 1:27
a [i] = i
end


julia> a0 = reshape(a, 9,3)
9x3 Array{Float64,2}:
 1.0  10.0  19.0
 2.0  11.0  20.0
 3.0  12.0  21.0
 4.0  13.0  22.0
 5.0  14.0  23.0
 6.0  15.0  24.0
 7.0  16.0  25.0
 8.0  17.0  26.0
 9.0  18.0  27.0

julia> writecsv ("a.txt", a0)

julia> a1 = readcsv ("a.txt"); ## a1 == a0 --> true

julia> a2 = reshape (a1, 3,3,3);

julia> a2 == a
true

Hope this helps,

Luis

paul analyst

unread,
Apr 4, 2014, 2:53:37 AM4/4/14
to juli...@googlegroups.com
Tim,
Julia docs is empty for "jld and "HDF5" . Do You mean this link : https://github.com/timholy/HDF5.jl ?
Paul

Ivar Nesje

unread,
Apr 4, 2014, 3:02:55 AM4/4/14
to juli...@googlegroups.com
Yes, he did.

paul analyst

unread,
Apr 4, 2014, 5:09:03 AM4/4/14
to juli...@googlegroups.com
Very good, thanks.
But if you can add new variables to an existing file jld?
Paul

Tim Holy

unread,
Apr 4, 2014, 5:35:34 AM4/4/14
to juli...@googlegroups.com
On Friday, April 04, 2014 02:09:03 AM paul analyst wrote:
> But if you can add new variables to an existing file jld?

Certainly. This is covered in the docs; you'll be interested in jldopen and
h5open.

Note that the README links to much longer files in the doc/ directory.

--Tim

paul analyst

unread,
Apr 4, 2014, 4:08:41 PM4/4/14
to juli...@googlegroups.com
I see , but:

t = 15 z = [1,3] @save "mydata.jld" t z

inside i have two variables: "t" "z" , nice...

now a need add next var (A) to the file

A=100;

file
= jldopen("mydata.jld", "w") @write file A close(file)

unfortunatly A removed vars "t" and "z" :/

Paul

Douglas Bates

unread,
Apr 4, 2014, 4:36:35 PM4/4/14
to juli...@googlegroups.com
Did you try opening the file in append mode?

paul analyst

unread,
Apr 5, 2014, 4:00:17 AM4/5/14
to juli...@googlegroups.com
OK, "r+".... but this info is little hiden :)

file
= jldopen("mydata.jld", "r+") @write file A close(file)

Tim Holy

unread,
Apr 5, 2014, 5:39:56 AM4/5/14
to juli...@googlegroups.com
On Saturday, April 05, 2014 01:00:17 AM paul analyst wrote:
> OK, "r+".... but this info is little hiden :)

https://github.com/timholy/HDF5.jl/blob/master/doc/hdf5.md#opening-and-
closing-files

Which aspect isn't clear? Is it the fact that basically everything about HDF5
also applies to JLD?

--Tim

Tim Holy

unread,
Apr 5, 2014, 5:41:26 AM4/5/14
to juli...@googlegroups.com
By the way, these kinds of questions are really julia-users questions ("how do
I use julia?"), not julia-dev ("how should we change julia?").

Best,
--Tim

On Saturday, April 05, 2014 01:00:17 AM paul analyst wrote:
Reply all
Reply to author
Forward
0 new messages