![](/img/trans.png)
[英]How to save big mat files in MATLAB with version newer than v7.3 and read it in Python
[英]Better dataformat than .mat v7.3 for import from Matlab 2016 to Mathematica 11?
我正在嘗試使用本地存儲將數據從Matlab 2016a / 2016b-prerelease導入到Mathematica 11
數據和質量保證
腳步
Matlab v7.3面臨的挑戰
自動方法的挑戰
Matlab的v7 .mat相對於v7.3已棄用
一些規范:將數據文件指定為v7.3的.mat文件
嘗試1
Matlab的
# Specify test data here time=0:0.001:1; potential=sin(time); C = spectrogram(potential); C=reshape(C,1,[]); C=nthroot( abs( C(1,1:1001) ), 1); hFig=figure(); hax=axes(hFig); imagesc(time,potential,C); filename=fullfile('/home/masi/Images/test'); filenameMat=fullfile('/home/masi/Images/test.mat'); export_fig(filename, '-tif', '-q101', '-a1', '-m1', '-RGB', '-nofontswap', '-nocrop', '-transparent', '-dpng', hax); save(filenameMat,'time', 'potential', 'C', '-v7.3');
Mathematica,其中#1-2都成功導入了許多變量
(* http://mathgis.blogspot.fi/2010/09/tips-import-matlab-mat-files.html *) (* https://mathematica.stackexchange.com/a/10589/9815 *) (* #1 Succeeds; select specific data sets *) mma = Import["~/Images/test.mat", {"HDF5", "Datasets", "/time"}]; (* #2 Succeeds: Out {"/C", "/potential", "/time"} *) mma = Import["~/Images/test.mat", {"HDF5", "Datasets"}]; (* Output: {{1.}, {1.5}, {2.}} *)
輸出:在Mathematica 11中,步驟(1-4)成功,但是數據文件(步驟5)的導入失敗,請參見上面的錯誤消息。
在Mathematica中讀取數據,其中Flatten
用於刪除一組括號,因為其中一組過多
(* https://stackoverflow.com/a/16834090/54964 *)
SetDirectory["Desktop"]
a = Import["m.mat"] ;
(* https://mathematica.stackexchange.com/a/97252/9815 *)
a=Partition[Flatten[a], 5000]
(* Output fails: {} *)
他只是在使用一個正方形矩陣。 我將數據包含在三個變量中: time
, potential
和C
,擬合imagesc()
的參數。
向量的平方矩陣的時間為 mx 1且電勢為 nx1。如何在平方矩陣A中應用向量C ? 我對這里的數學理解不足,無法自己回答問題。
# time's dimensions mx 1 # (potential')'s dimensions 1 xn time=0:0.001:1; potential=sin(time); A = time' * potential; # Output: A is mxn matrix, which is as as expected. # C is vector 1 xm here. C = spectrogram(potential); C=reshape(C,1,[]); C=nthroot( abs( C(1,1:1001) ), 1);
如何將方陣A(C)轉換回這三個變量? -( A)關於平方矩陣,其中向量C已應用於平方矩陣A。 我不了解創建結果的背后的數學原理。
Matlab:2016a,2016b預發布
Mathematica:11
操作系統:Debian 8.5
相關: 有沒有一種方法可以將結果或數據從Matlab自動導入到Mathematica?
從matlab到mathematica交換原始二進制文件的示例:
Matlab的:
mat = [ pi 2*pi 3*pi ; 1 sqrt(2) sqrt(3) ]
f=fopen('out.bin','w')
fwrite(f,size(mat))
fwrite(f,mat,'double')
... # repeat for however many matrices we need to write
fwrite(f,size(mat2))
fwrite(f,mat2,'double')
...
fclose(f)
數學:
f = OpenRead["out.bin", BinaryFormat -> True];
size = BinaryReadList[f, "Integer8", 2];
mat = Transpose@ArrayReshape[
BinaryReadList[f, "Real64",Times@@size],
Reverse@size];
(* repeat as needed to read multiple matrices *)
Close[f];
MatrixForm@mat
請注意,需要Reverse
和Transpose
,因為matlab以列主要順序寫入數據。 您可以在編寫時交替執行fwrite(f,transpose(mat),'double')
。
請注意,這也假定為正方形陣列。 如果要處理多維數組,則還需要將length(size)
寫入文件,依此類推。
為了完整起見,請像這樣返回:
f = OpenWrite["out.bin", BinaryFormat -> True];
BinaryWrite[f, Dimensions[mat], "Integer8"];
BinaryWrite[f, Transpose[mat], "Real64"];
Close[f]
..
f=fopen('out.bin','r')
sz=transpose(fread(f,2))
mat=fread(f,sz,'double')
fclose(f)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.