該文件是一個輸出文件,其第一行是一個標題,後面跟着帶有數據的n
行。第一組數據後面跟着更多類似的具有不同數值的數據組。如何閱讀這個文件(重複模式)列明智的Matlab?
我想讀此文件中的第2和第3列的數據,即direction 1
,direction 2
等。目前我使用的是函數內的以下幾行代碼來讀取數據的所有集合,如下圖所示:
fid = fopen(output_file); % open the output file
dotOUT_fileContents = textscan(fid,'%s','Delimiter','\n'); % read it as string ('%s') into one big array, row by row
dotOUT_fileContents = dotOUT_fileContents{1};
fclose(fid); %# close the file
%# find rows containing 'SV'
data_starts = strmatch('SV',...
dotOUT_fileContents); % data_starts contains the line numbers wherever 'str2match' is found
nDataRows=data_starts(2)-data_starts(1)-1;
ndata = length(data_starts); % total no. of data values will be equal to the corresponding no. of 'str2match' read from the .out file
%# loop through the file and read the numeric data
for w = 1:ndata
%# read lines containing numbers
tmp_str = dotOUT_fileContents(data_starts(w)+1:data_starts(w)+nDataRows);
%# convert strings to numbers
y = cell2mat(cellfun(@(z) sscanf(z,'%f'),tmp_str,'UniformOutput',false)); % store the content of the string which contains data in form of a character
data_matrix_column_wise(:,w) = y; % convert the part of the character containing data into number
%# assign output in terms of lag and variogram values
lag_column_wise(:,w)=data_matrix_column_wise(2:6:nLag*6-4,w);
vgs_column_wise(:,w)=data_matrix_column_wise(3:6:nLag*6-3,w);
end
如果我沒有在上面的輸出文件中顯示星星,此功能運行良好。但是,上面顯示的輸出文件之一包含星號,上面的代碼在這種情況下失敗。應該怎樣處理數據中的星號以便我能夠正確讀取第2列和第3列?
不客氣 - 祝你好運! – Ansari 2012-07-13 19:47:48