在Matlab中读取文本文件(数据传输) [英] Read text file in matlab (data trancation)

查看:262
本文介绍了在Matlab中读取文本文件(数据传输)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在读取matlab中的文本文件.这是代码

I am reading a text file in matlab . Here is the code

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

the second column value is truncated however, I want to get 0.3724127064860939

推荐答案

有多种方式读取此类数据(以空格分隔).在所有方法中,都保留精度.

There are various ways to read this sort of data (space-delimited). In all methods precision is preserved.

假设您在demo.txt中输入了内容:

Assuming you have the input in demo.txt:

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');

这篇关于在Matlab中读取文本文件(数据传输)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆