Matlab是否有类似于R中dput()函数的等效函数?

11

是否有Matlab中类似于R的dput()函数?

dput()函数将一个R对象的ASCII文本表示写入文件或连接。


你是否考虑编写二进制表示? - Roman Byshko
不需要 - 只是想要一种像R的dput()一样简单的方法来发送或发布结构化数据。 - Ram Ahluwalia
@QuantGuy 请看我的回答。只需让我知道您需要哪些其他数据类型,我会尝试添加它们。顺便说一句,这是个好主意! - John Colby
3个回答

10

更新1:添加了递归和对单元格的支持!

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

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

注意:请查看github以获取更多更新:https://github.com/johncolby/dput


虽然没有内置的等效功能,但创建它的模板足够简单,所以我想开始制作它。只需循环遍历变量并根据数据类型编写字符串等效项即可。

我为此开始了一个git库,因此随时欢迎分叉并帮助处理不同的数据类型。当基本类型完成(至少包括double、char、struct、cell)时,我将在FileExchange上发布它。

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};

基本用法如下:

>> 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])                                                                                                                                                                                                                                                                                                                                                                                                                                             ;

那么你只需要将文本在线粘贴以创建可重现的示例,其他人可以将其复制/粘贴回MATLAB中重新生成变量。就像对于R一样!


1
太棒了。顺便通过 GitHub 对整个社区做出巨大的贡献,再次感谢! - Ram Ahluwalia
当我尝试获取dput-style东西时,我遇到了错误:http://pastebin.com/Atz696me - hhh
John:非常好的答案,对于那些从R转来的人非常有帮助。不知道您是否考虑添加时间序列对象的支持?我是MatLab的新手,如果我自己能弄清楚,我会把它发送过来的。 - LGTrader
我知道“感谢”评论不被看好,但是...哇,这是一个非常有帮助的工具。 - Carl Witthoft

4
显然,这个问题假设你已经安装了工作的Matlab。如果你想使用Matlab对象中的数据来构建R中的示例,则可以在“R.matlab”包中使用readMat。你可以使用R从Matlab文件中提取数据(或使用服务器连接),然后使用dputdump
在仅使用Matlab的情况下,至少根据我阅读文档的理解,我所看到的选项是(它可能仅适用于Matlab矩阵):
filename='asc.txt'
save(filename, 'mat', '-ascii')
type asc.txt

还有一种选项(虽然不太符合ASCII的精神),即使用通用数据格式,其中有Matlab-write和R-read函数。


我相信OP实际上希望能够像创建R示例一样轻松地构建MATLAB示例。这正确吗? - John Colby
MATLAB的save()确实有一个限制,即“每个变量必须是二维double或char数组”。此外,它们不能被粘贴,因此在发布易用性方面与使用正常的二进制.mat表示没有任何优势。 - John Colby

0
>> 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])                                                                                                                                                                                                                                                                                                                                                                                                                                             ;

然后您可以将文本粘贴到在线环境中,以创建可重现的示例,其他人可以将其复制/粘贴回MATLAB中以重新生成变量。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接