我想閱讀下列格式重演文件(但我已經切出的數據甚至因爲過長的第一次重複):Matlab:如何使用`sscanf`讀取包含小數的數字?
1.00 'day' 2011-01-02
'Total Velocity Magnitude RC - Matrix' 'm/day'
0.190189 0.279141 0.452853 0.61355 0.757833 0.884577
0.994502 1.08952 1.17203 1.24442 1.30872 1.36653
1.41897 1.46675 1.51035 1.55003 1.58595 1.61824
這是原來的file
在一些數據文件中,我有十進制數字的日子。在這種情況下,我只是得到小數點前的數字,沒有小數。我正在使用下面顯示的代碼來讀取和存儲。 如何在days
中包含小數部分?比如我想days
存儲的2.2,而不是僅僅2
fid = fopen(file_name); % open the file
dotTXT_fileContents = textscan(fid,'%s','Delimiter','\n'); % read it as string ('%s') into one big array, row by row
dotTXT_fileContents = dotTXT_fileContents{1};
fclose(fid); %# don't forget to close the file again
% str2match = '''Total Velocity Magnitude RC - Matrix'' ''m/day''';
%# find rows containing 'Total Velocity Magnitude RC - Matrix' 'm/day'
data_starts = strmatch(str2match,...
dotTXT_fileContents); % data_starts contains the line numbers wherever 'Total Velocity Magnitude RC - Matrix' 'm/day' is found
nDataRows=data_starts(2)-data_starts(1);
ndata = length(data_starts); % total no. of data values will be equal to the corresponding no. of '** K' read from the .txt file
%# loop through the file and read the numeric data
for w = 1:ndata
%# read lines containing numbers
tmp_str = dotTXT_fileContents(data_starts(w):data_starts(w)+nDataRows-2); % stores the content from file dotTXT_fileContents of the rows following the row containing 'Total Velocity Magnitude RC - Matrix' 'm/day' in form of string
%# 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
%# assign output
data_matrix_column_wise(:,w) = y; % convert the part of the character containing data into number
%# reading date in days passed since beginning of simulation
days_str = dotTXT_fileContents(data_starts(w)-1);
days_str = days_str{1};
days(w) = sscanf(days_str, '%d');
end