簡體   English   中英

Matlab 中是否有 R 的 dput() 等價物?

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

Matlab 中是否有 R 的 dput() 等價物?

dput() 將 R 對象的 ASCII 文本表示寫入文件或連接。

更新 1 :添加了對單元格的遞歸和支持!

更新 2 :增加了對結構的支持!

更新 3 :添加了對邏輯、整數、復數雙精度的支持。 添加了單元測試。 發布到 FileExchange: http : //www.mathworks.com/matlabcentral/fileexchange/34076

注意:在https://github.com/johncolby/dput 上查看 github 以獲取所有進一步更新。


沒有內置的等價物,但創建一個的模板很簡單,所以我想我會開始制作它。 只需遍歷變量並根據數據類型編寫等效的字符串。

我為此創建了一個 git 存儲庫,所以請隨意 fork 並幫助我處理不同的數據類型。 當基本類型完成后(至少是 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 一樣!

這個問題顯然假設了一個有效的 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 函數。

>> 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 以重新生成變量。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM