从 .txt 文件读取值到 Matlab 中的向量
Read values from .txt file to vectors in Matlab
我在一个文本文件中有数据,文本文件的每一行包含不同数量的列。我的数据格式如下:
2
21 2623
707 40 1
连续三个数字后,数据保持相同的结构,直到文件结束。我想处理数据,所以我有三个向量 X、Y 和 Z,其中包含来自具有三个数字的行的值,因此:
X = 707
Y = 40
Z = 1
非常感谢!
如果不需要存储前三行,可以使用textscan
指定 属性 'headerlines' 3
或 dlmrear 指定 属性 'roffset'
4.
希望对您有所帮助。
假设要忽略的行数不是常量,并且您想以编程方式而不是使用导入向导来执行此操作,您首先需要计算出有多少行有忽略的。
一种方法是使用 fgetl
, which goes through a file line-by-line. Once you have the number of headers you can then read in the data with a function like dlmread
or textscan
.
例如:
fID = fopen(myfilepath, 'r'); % Open data file for reading
ncoldesired = 3;
ii = 0;
% Search for the first row of "good" data
while ~feof(fID) % Loop will stop if it reaches the end of the file
tline = fgetl(fID);
temparray = str2num(tline); % Convert string to array of doubles, could also use regex
if length(temparray) == ncoldesired
% Found the first row of data
nheaders = ii;
fclose(fID);
break
end
ii = ii + 1;
end
% Error Catching
if exist('nheaders', 'var') == 0 && feof(fID)
fclose(fID);
error('End of file reached, no good data found');
end
mydata = dlmread(myfilepath, ' ', nheaders, 0); % For some reason dlmread uses zero-based indexing unlike everything else in MATLAB
rowContainsThreeElems = cellfun(@(x) size(x,2)==3, tline_c);
% // Get those rows
A= cell2mat(tline_c(rowContainsThreeElems==1))
A =
707 40 1
% // Data.txt
% // --------------
% // 2
% // 21 2623
% // 707 40 1
% // Read in the file and convert it to an array
fid = fopen('data.txt','r');
tline = fgets(fid);
tline_c = {};
n = 1;
while ischar(tline)
disp(tline)
% // Convert the line to a numeric array but store it in a cell array
tline_c{n} = str2num(tline);
tline = fgets(fid);
n=n+1;
end
fclose(fid);
% // Get the lines that have three elements
rowContainsThreeElems = cellfun(@(x) size(x,2)==3, tline_c);
rowContainsThreeElems =
0 0 1
% // Get those rows
A= cell2mat(tline_c(rowContainsThreeElems==1));
ans =
707 40 1
% // Add them to X,Y,Z
X=A(:,1); Y=A(:,2); Z=A(:,3);
我在一个文本文件中有数据,文本文件的每一行包含不同数量的列。我的数据格式如下:
2
21 2623
707 40 1
连续三个数字后,数据保持相同的结构,直到文件结束。我想处理数据,所以我有三个向量 X、Y 和 Z,其中包含来自具有三个数字的行的值,因此:
X = 707
Y = 40
Z = 1
非常感谢!
如果不需要存储前三行,可以使用textscan
指定 属性 'headerlines' 3
或 dlmrear 指定 属性 'roffset'
4.
希望对您有所帮助。
假设要忽略的行数不是常量,并且您想以编程方式而不是使用导入向导来执行此操作,您首先需要计算出有多少行有忽略的。
一种方法是使用 fgetl
, which goes through a file line-by-line. Once you have the number of headers you can then read in the data with a function like dlmread
or textscan
.
例如:
fID = fopen(myfilepath, 'r'); % Open data file for reading
ncoldesired = 3;
ii = 0;
% Search for the first row of "good" data
while ~feof(fID) % Loop will stop if it reaches the end of the file
tline = fgetl(fID);
temparray = str2num(tline); % Convert string to array of doubles, could also use regex
if length(temparray) == ncoldesired
% Found the first row of data
nheaders = ii;
fclose(fID);
break
end
ii = ii + 1;
end
% Error Catching
if exist('nheaders', 'var') == 0 && feof(fID)
fclose(fID);
error('End of file reached, no good data found');
end
mydata = dlmread(myfilepath, ' ', nheaders, 0); % For some reason dlmread uses zero-based indexing unlike everything else in MATLAB
rowContainsThreeElems = cellfun(@(x) size(x,2)==3, tline_c);
% // Get those rows
A= cell2mat(tline_c(rowContainsThreeElems==1))
A =
707 40 1
% // Data.txt
% // --------------
% // 2
% // 21 2623
% // 707 40 1
% // Read in the file and convert it to an array
fid = fopen('data.txt','r');
tline = fgets(fid);
tline_c = {};
n = 1;
while ischar(tline)
disp(tline)
% // Convert the line to a numeric array but store it in a cell array
tline_c{n} = str2num(tline);
tline = fgets(fid);
n=n+1;
end
fclose(fid);
% // Get the lines that have three elements
rowContainsThreeElems = cellfun(@(x) size(x,2)==3, tline_c);
rowContainsThreeElems =
0 0 1
% // Get those rows
A= cell2mat(tline_c(rowContainsThreeElems==1));
ans =
707 40 1
% // Add them to X,Y,Z
X=A(:,1); Y=A(:,2); Z=A(:,3);