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

问题描述:

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

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

+0

你还考虑写二进制表示吗? – Beginner

+0

不 - 只是寻找一种简单的方式来发送电子邮件或发布结构化数据,与R的输入()相同() –

+0

@QuantGuy请参阅下面的答案。请让我知道您需要的其他数据类型,然后我会尝试添加下一个。顺便说一句好主意! –

UPDATE 1:增加了对单元格的递归和支持!

UPDATE 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贡献给广大社区的贡献,再次感谢! –

+0

当我尝试获取dput风格的东西时,出现错误:http://pastebin.com/Atz696me – hhh

+0

John:很好的答案,对于来自R的用户非常有帮助。不知您是否会考虑添加对时间序列对象?我是MatLab的新手,如果我能自己弄清楚,我会发送它。 – LGTrader

这个问题显然假定一个可行的Matlab安装。如果要使用Matlab对象中的数据构造R中的示例,“R.matlab”包中显然会有readMat。您可以使用R从Matlab文件(或使用服务器连接)提取数据,然后使用dputdump

短短MATLAB,至少accoring我阅读文档,我看到选项(并且可能仅适用于Matlab的矩阵)是

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

还有一个选项(虽然不是真的本着ASCII的精神)使用具有Matlab写入和R读取功能的通用数据格式。

+0

我相信OP实际上想要构建MATLAB示例,并且可以轻松创建R示例。它是否正确? –

+0

MATLAB的'save()'确实有一个限制,即“每个变量必须是一个二维双精度数组或char数组”。此外,这些不是可粘贴的,所以在使用普通的二进制.mat表示方式时,并没有任何优势来发布易用性。 –