是否有Matlab中类似于R的dput()函数?
dput()函数将一个R对象的ASCII文本表示写入文件或连接。
更新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一样!
readMat
。你可以使用R从Matlab文件中提取数据(或使用服务器连接),然后使用dput
或dump
。filename='asc.txt'
save(filename, 'mat', '-ascii')
type asc.txt
还有一种选项(虽然不太符合ASCII的精神),即使用通用数据格式,其中有Matlab-write和R-read函数。
save()
确实有一个限制,即“每个变量必须是二维double或char数组”。此外,它们不能被粘贴,因此在发布易用性方面与使用正常的二进制.mat表示没有任何优势。 - John Colby>> 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中以重新生成变量。