Matlab有相当于R的dput()吗? [英] Is there an equivalent of R's dput() for Matlab?

查看:104
本文介绍了Matlab有相当于R的dput()吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

对于Matlab,R的dput()是否等效?

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

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

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

推荐答案

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

UPDATE 1: Added recursion and support for cells!

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

UPDATE 2: Added support for structures!

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

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

注意:在 https://github.com/johncolby/dput 获取所有进一步的更新.

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.

我为此启动了一个git存储库,因此可以随意进行分叉并帮助我处理不同的数据类型.基本类型完成后(至少是double,char,struct,cell),我将其发布在FileExchange上.

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

从一些示例变量开始

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!

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!

这篇关于Matlab有相当于R的dput()吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆