简体   繁体   English

无法使用textread在Matlab中读取文件数据

[英]Can't read file data in Matlab with textread

I'm trying to read a .samp file's data in Matlab with the textread function. 我正在尝试使用textread函数在Matlab中读取.samp文件的数据。 I want to put each row of the file in an array. 我想将文件的每一放在一个数组中。 This is the .samp file: 这是.samp文件:

@CATEGORY:GENERAL
@IMAGE:2012-04-26-Muenchen-Tunnel_4K0G0010.JPG
# format: id type center.x center.y size.width size.height angle
0 30 1319 2338 35 11 56.451578
1 30 1337 2350 42 14 57.817368
2 30 224 3556 61 20 136.967797

When I try to run this command: 当我尝试运行此命令时:

[id, type, x, y, width, height, angle] = textread('data', '%d%d%d%d%d%d%f', 'headerlines', 3);

it doesn't work and gives an error: 它不起作用并给出错误:

Error using dataread 使用数据读取时出错

Trouble reading integer from file (row 1, field > 1) ==> % CB = DATA(OBJ, 'get_callbacks') returns a 无法从文件中读取整数(行1,字段> 1)==>%CB = DATA(OBJ,'get_callbacks')返回一个

Error in textread (line 174) 文本阅读错误(第174行)

[varargout{1:nlhs}]=dataread('file',varargin{:}); [varargout {1:nlhs}] = dataread('file',varargin {:}); %#ok %#好

How should I type the command to run correctly? 如何键入命令才能正确运行? and what should I do afterwards to put each row (line) in a separate array? 然后我应该怎么做将每一行(行)放在一个单独的数组中?

Try to specify the full file path as the first argument. 尝试将完整的文件路径指定为第一个参数。 For me, putting data in C:\\Users\\MyUsername\\ and then calling 对我来说,将data放入C:\\Users\\MyUsername\\ ,然后调用

 [id, type, x, y, width, height, angle] = textread('C:\Users\MyUsername\data',...
                                                   '%d%d%d%d%d%d%f',...
                                                   'headerlines', 3);

was pretty much enough to make it work. 足以使其工作。 However, Matlab documentation suggests switching textread to textscan , so you should probably consider that. 但是,Matlab 文档建议将textread切换为textscan ,因此您可能应该考虑这一点。

As for the second part of your question: do you actually expect to have just three lines in you file? 至于问题的第二部分:您实际上希望文件中只有三行吗? If not, you could put all your column vectors into a matrix and access horizontal vectors by slicing: 如果没有,您可以将所有列向量放入矩阵中,并通过切片来访问水平向量:

M = [id type x y width height angle];
M(1,:) %  first line of your file

However, if you do expect to have just three lines, you could use 但是,如果您确实希望只有三行,则可以使用

vec1 = [id(1) type(1) x(1) y(1) width(1) height(1) angle(1)];
vec2 = [id(2) type(2) x(2) y(2) width(2) height(2) angle(2)];
vec3 = [id(3) type(3) x(3) y(3) width(3) height(3) angle(3)];

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM