4

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

To read a 2-dimensional matrix is trivial. I figured that since my first dimension is only 4-deep, I could just write each slice of it out to a separate file (4 files total) with each file having many 2-dimensional slices, 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 a single continuous 6 x 3 matrix. I even took a look a dlmread but could not figure out how to get it do what I wanted. How do I format this so I can put 3 (or preferably more) dimensions in a single file?

2 Answers 2

7

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

Say your file is

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

You do the following to read the array into the variable data

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

Have a look at data to see how Matlab orders elements in multidimensional arrays.

Sign up to request clarification or add additional context in comments.

1 Comment

Excellent solution, I approve heartily. Thank you very much.
2

Matlab stores data column wise. So from your example (assuming its a 3x2x3 matrix), matlab will store it as first, second and third column from the first "slice", followed by the first, second third columns from the second slice and so on like this

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

So you can write the data out like this from python (I don't know how) and then read it into matlab. Then you can reshape it back into a 3x2x3 matrix and you'll retain your correct ordering.

3 Comments

This is getting there... I see how to do it this way now, but it would require me figuring out how deep each of my dimensions is manually (which varies from execution to execution). Is there any way of encoding this information directly into the input file?
you could probably write it to a custom file with a header, where you store the info. A simple e.g., the first line of the file in this case will be 3 (for 3 dimensions), next 3 lines will be 3, 2 and 3 respectively. So when you read in matlab, you read the first line, then read the next how-ever many lines. This tells you exactly how many rows/cols/dimensions are there. After reading these, you read till the very end of the file and then reshape using this info
close, but no cigar I guess :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.