简体   繁体   中英

Is there an equivalent of R's dput() for Matlab?

Is there an equivalent of R's dput() for Matlab?

The dput() writes an ASCII text representation of an R object to a file or connection.

UPDATE 1 : Added recursion and support for cells!

UPDATE 2 : Added support for structures!

UPDATE 3 : Added support for logicals, integers, complex doubles. Added unit tests. Posted to FileExchange at: http://www.mathworks.com/matlabcentral/fileexchange/34076

NOTE : Check github at https://github.com/johncolby/dput for all further updates.


There is no built-in equivalent, but the template to create one is simple enough, so I thought I'd start making it. Just loop over the variables and write a string equivalent depending on the type of the data.

I started a git repository for this, so feel free to fork it and help me out with different data types. I'll post it on FileExchange when the basic types are complete (double, char, struct, cell at least).

https://github.com/johncolby/dput

Starting with some example variables

x = 1:10;
y = 3;
z = magic(3);
mystr = ['line1'; 'line2'];
mystruct = mystruct = struct('index', num2cell(1:3), 'color', {'red', 'blue', 'green'}, 'misc', {'string' 4 num2cell(magic(3))})
mycell = {1:3, 'test'; [], 1};

the basic usage is:

>> dput(x, y, z, mystr, mystruct, mycell)

ans =

x        = reshape([1.000000 2.000000 3.000000 4.000000 5.000000 6.000000 7.000000 8.000000 9.000000 10.000000 ],[1  10])                                                                                                                                                                                                                                                                                                                                                                                                                                                                      ;
y        = reshape([3.000000 ],[1  1])                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                         ;
z        = reshape([8.000000 3.000000 4.000000 1.000000 5.000000 9.000000 6.000000 7.000000 2.000000 ],[3  3])                                                                                                                                                                                                                                                                                                                                                                                                                                                                                 ;
mystr    = reshape('lliinnee12',[2  5])                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                        ;
mystruct = struct('index',reshape({reshape([1.000000 ],[1  1]) reshape([2.000000 ],[1  1]) reshape([3.000000 ],[1  1]) },[1  3]),'color',reshape({reshape('red',[1  3]) reshape('blue',[1  4]) reshape('green',[1  5]) },[1  3]),'misc',reshape({reshape('string',[1  6]) reshape([4.000000 ],[1  1]) reshape({reshape([8.000000 ],[1  1]) reshape([3.000000 ],[1  1]) reshape([4.000000 ],[1  1]) reshape([1.000000 ],[1  1]) reshape([5.000000 ],[1  1]) reshape([9.000000 ],[1  1]) reshape([6.000000 ],[1  1]) reshape([7.000000 ],[1  1]) reshape([2.000000 ],[1  1]) },[3  3]) },[1  3]));
mycell   = reshape({reshape([1.000000 2.000000 3.000000 ],[1  3]) reshape([ ],[0  0]) reshape('test',[1  4]) reshape([1.000000 ],[1  1]) },[2  2])                                                                                                                                                                                                                                                                                                                                                                                                                                             ;

Then you can just paste the text online to make a reproducible example, and others can copy/paste back into MATLAB to regenerate the variables. Just like for R!

The question obviously presumes a working Matlab installation. If you want to construct examples in R using data in Matlab objects, there is obviously readMat in the "R.matlab" package. You could extract data from a Matlab file (or use a server connection) with R and then use dput or dump .

Within just Matlab, at least accoring to my reading of the documentation, the option I see (and it may be only applicable to Matlab matrices) is

filename='asc.txt'
save(filename, 'mat', '-ascii')
type asc.txt

There is also the option (although not really in the spirit of ASCII) of using the Common Data Format for which there are Matlab-write and R-read functions.

>> dput(x, y, z, mystr, mystruct, mycell)

ans =

x        = reshape([1.000000 2.000000 3.000000 4.000000 5.000000 6.000000 7.000000 8.000000 9.000000 10.000000 ],[1  10])                                                                                                                                                                                                                                                                                                                                                                                                                                                                      ;
y        = reshape([3.000000 ],[1  1])                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                         ;
z        = reshape([8.000000 3.000000 4.000000 1.000000 5.000000 9.000000 6.000000 7.000000 2.000000 ],[3  3])                                                                                                                                                                                                                                                                                                                                                                                                                                                                                 ;
mystr    = reshape('lliinnee12',[2  5])                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                        ;
mystruct = struct('index',reshape({reshape([1.000000 ],[1  1]) reshape([2.000000 ],[1  1]) reshape([3.000000 ],[1  1]) },[1  3]),'color',reshape({reshape('red',[1  3]) reshape('blue',[1  4]) reshape('green',[1  5]) },[1  3]),'misc',reshape({reshape('string',[1  6]) reshape([4.000000 ],[1  1]) reshape({reshape([8.000000 ],[1  1]) reshape([3.000000 ],[1  1]) reshape([4.000000 ],[1  1]) reshape([1.000000 ],[1  1]) reshape([5.000000 ],[1  1]) reshape([9.000000 ],[1  1]) reshape([6.000000 ],[1  1]) reshape([7.000000 ],[1  1]) reshape([2.000000 ],[1  1]) },[3  3]) },[1  3]));
mycell   = reshape({reshape([1.000000 2.000000 3.000000 ],[1  3]) reshape([ ],[0  0]) reshape('test',[1  4]) reshape([1.000000 ],[1  1]) },[2  2])                                                                                                                                                                                                                                                                                                                                                                                                                                             ;

Then you can just paste the text online to make a reproducible example, and others can copy/paste back into MATLAB to regenerate the variables.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM