我正在阅读matlab
的文本文件。这是代码
allData = textread(file', '%s', 'delimiter', 'n');
numericalArray = cellfun(@(s) sscanf(s,'%f').' ,allData, 'un', 0);
% Get Header
header = allData(cellfun('isempty',numericalArray));
% Get Data
data = vertcat(numericalArray{:});
下面是示例文本文件
head1 head2
760.00 0.3724127064860939
输出:
data(1,:)
ans =
760.0000 0.3724
第二列值被截断,但是,我想得到0.3724127064860939
有多种方法1:简单
方法2:
方法3:
方法可以读取此类数据(空格分隔)。在所有方法中都保持精度。
假设您有demo.txt
输入:
方法1:简单textscan
fid = fopen('demo.txt','r'); % open for reading
txt = textscan(fid,'%s','delimiter', 'n'); txt = txt{1}; % read lines and unbox
fclose(fid);
H = strsplit(txt{1},' '); % split the headers line
V = str2double(strsplit(txt{2},' '));
方法2:dlmread
R = dlmread('demo.txt',' ',1,0); % this is an example in the documentation of dlmread
方法3:readtable
T = readtable('demo.txt');