What format does Matlab require to enter n-dimensional data?

I have a 4-dimensional dictionary that I made with a Python script for the data mining project I'm working on, and I want to read the data in Matlab to do some statistical data tests.

Reading a two-dimensional matrix is ​​trivial. I realized that since my first dimension has only 4 depths, I could just write each fragment of it into a separate file (only 4 files), and each file has many two-dimensional fragments, looking something like this:

2 3 6 4 5 8 6 7 3 1 4 3 6 6 7 8 9 0 

This, however, does not work, and Matlab reads it as one continuous 6 x 3 matrix. I even looked at dlmread, but could not figure out how to get it to do what I wanted. How to do this so that I can place 3 (or preferably more) sizes in one file?

+4
source share
2 answers

A simple solution is to create a file with only two lines: the first line contains the size of the target array, the second line contains all your data. Then all you have to do is change the data.

Say your file

 3 2 3 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 

You should do the following to read the array in the data variable

 fid = fopen('myFile'); %# open the file (don't forget the extension) arraySize = str2num(fgetl(fid)); %# read the first line, convert to numbers data = str2num(fgetl(fid)); %# read the second line data = reshape(data,arraySize); %# reshape the data fclose(fid); %# close the file 

See data to see how Matlab orders elements in multidimensional arrays.

+7
source

Matlab stores a data column. So, from your example (suppose it's a 3x2x3 matrix), Matlab will save it as the first, second and third columns from the first β€œslice”, followed by the first, second third columns from the second slice, and so on.

 2 4 3 5 6 8 6 1 7 4 3 3 6 8 6 9 7 0 

So, you can write data from this list from python (I don't know how), and then read it in matlab. Then you can reshape put it back into the 3x2x3 matrix and you will keep the correct order.

+2
source

All Articles