我正在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
答案 0 :(得分:0)
有多种方法可以读取此类数据(以空格分隔)。在所有方法中,精度都得以保留。
假设您有demo.txt
的输入:
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},' '));
dlmread
R = dlmread('demo.txt',' ',1,0); % this is an example in the documentation of dlmread
readtable
T = readtable('demo.txt');