我正在嘗試在 Matlab 中加載檔案。但我對最好的方法感到有些困惑。
該檔案有 3 列,如下圖所示:

這個檔案我可以通過執行load('c').
但是,我不得不在底行添加 2 個 NaN。
原始檔案實際上看起來像下面的檔案:

現在,如果我load('c')對下面的檔案進行操作,則會收到錯誤訊息:
Error using load
Unable to read file 'c'. Input must be a MAT-file or an ASCII file containing numeric
data with same number of columns in each row.
當然我可以使用ImportData 來匯入這個檔案,但是匯入它實在是太慢了。
有什么建議?
uj5u.com熱心網友回復:
您應該可以使用c = readtable('c'). 默認情況下,這應該會自動將空條目更改為“NaN”,但如果不是,則可以在選項中進行設定。
如果我有一個難以匯入的檔案(在readtable()……之前……這在過去幾年中使事情變得更容易),我會經常使用“匯入資料”工具(如果它是一個非常大的檔案,您可以制作一個模擬 -復雜的檔案以使其加載速度更快)然后根據需要更改所有匯入設定,然后綠色復選顯示“匯入選擇”的位置使用黑色下拉箭頭選擇“生成函式”。這將為您提供設定所有內容的編碼方式,以按照您想要的方式獲取檔案。
load() 更適合讀取在 Matlab 中創建的先前保存的“.mat”檔案。
uj5u.com熱心網友回復:
這是一種低級方法,它可能比其他方法更快:
filename = 'c'; % name of the file
N = 3; % number of columns
fid = fopen(filename, 'r'); % open file for reading
x = fscanf(fid, '%f'); % read all values as a column vector
fclose(fid); % close file
x = [x; NaN(N-mod(numel(x)-1,N)-1, 1)]; % include NaN's to make length a multiple of N
x = reshape(x, N, []).'; % reshape to N columns in row-major order
轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/389985.html
上一篇:Matlab命令對于矩陣是唯一的
