是否有相当于R的dput()用于Matlab?

Ram*_*lia 9 matlab r matrix

是否有相当于R的dput()用于Matlab?

dput()将R对象的ASCII文本表示写入文件或连接.

Joh*_*lby 10

更新1:为细胞添加了递归和支持!

更新2:增加了对结构的支持!

更新3:增加了对逻辑,整数,复数双精度的支持.添加了单元测试.发布到FileExchange:http://www.mathworks.com/matlabcentral/fileexchange/34076

注意:请访问https://github.com/johncolby/dput查看github以获取所有进一步的更新.


没有内置的等价物,但创建一个的模板很简单,所以我想我会开始制作它.只需循环遍历变量并根据数据类型编写等效字符串.

我为此启动了一个git存储库,所以请随意分叉并帮助我使用不同的数据类型.当基本类型完成时,我会在FileExchange上发布它(至少是double,char,struct,cell).

https://github.com/johncolby/dput

从一些示例变量开始

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};
Run Code Online (Sandbox Code Playgroud)

基本用法是:

>> 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])                                                                                                                                                                                                                                                                                                                                                                                                                                             ;
Run Code Online (Sandbox Code Playgroud)

然后,您可以在线粘贴文本以生成可重现的示例,其他人可以复制/粘贴回MATLAB以重新生成变量.就像R一样!