Håkon Hægland
Håkon Hægland

Reputation: 40718

Linux utility program to write a matlab .mat file

Consider the following text file file:

1 2 3
4 5 6
7 8 9

I would like to have linux command writemat that does the following:

$ writemat --size=3,3 --outputFile=m.mat --name=A <file

producing a matlab .mat file m.mat, such that I can later do from within Matlab:

>> s=load('m.mat')

s = 

    A: [3x3 double]

>> s.A

ans =

     1     2     3
     4     5     6
     7     8     9

Does such a command exist? Or do I have to write my own using the Matlab Application Programming interfaces? However, the latter feels somewhat like reinventing the wheel..

Upvotes: 1

Views: 893

Answers (1)

Luis Mendo
Luis Mendo

Reputation: 112659

This is not exactly what you want, but it may be equivalent: In Matlab you can load an ASCII file (like that in your example) using

load(name,'-ascii')

where name is a string containing the file name. The file contents are read into a Matlab variable with the same name as the file.


If you need several variables in the same file, you could write the ASCII file as

a=[
1 2 3
4 5 6
7 8 9];
b=[
0 1 0 1];

(that is, add variable names and [, ], ; signs), and then run it from Matlab.

Upvotes: 1

Related Questions