如果行在 .txt 文件中移动,则使用 readtable 导入 table
Import table with readtable if row is shifted in .txt file
我有一个 table 看起来像这样
x x x x x x
x x
y y y y y y
y y
z z z z z z
z z
我想使用 readtable
导入它,这样所有的 x 都在一行中,所有的 y 在下一行中,等等。换句话说,在 .txt 文件中,最后两个内容应该在一行中被转移到下一行。我想我需要更改 DelimitedTextImportOptions
中的某些内容,但我不知道具体是什么。
如果有人能帮助我,我会很高兴,非常感谢!
如果需要使用 readtable,一种选择是将原始文件转换为新格式,然后将 readtable 应用于新文件。
以下是可用于以下示例的文件 in.txt
的示例内容:
1 2 3 abc 5 6
7 8
3 4 5 def 7 8
9 0
9 1 0 ghi 3 2
1 4
代码如下:
% FIRST, TRANSFORM THE INPUT FILE INTO A FILE WHERE THE SPLIT LINES ARE
% COMBINED INTO SINGLE LINES
% open input and output files
in = fopen('in.txt', 'r');
out = fopen('out.txt', 'w');
% read the first line of the input file
currline = fgetl(in);
% while we haven't reached the end of the file
while ~isequal(currline, -1)
% read the following line of the input file
currline_append = fgetl(in);
% ... if it doesn't exist, throw an error; the file is not as expected
if isequal(currline_append, -1)
error('Bad file');
end
% print this pair of lines to the output file as a single line.
% Note: if using Windows Notepad or similar application to read the
% file, you may want to replace '\n' by '\r\n' in the format string
fprintf(out, '%s %s\n', currline, currline_append);
% get the next line of the input file
currline = fgetl(in);
end
% close input and output files
fclose(in);
fclose(out);
% NEXT, READ THE TABLE FROM THE OUTPUT FILE
t = readtable('out.txt');
实际上,如果您的文本文件的形状如您所描述的 EXACTLY 和 STRICTLY (每行具有相同数量的元素,并且其中两个溢出到下一行),你可以很容易地阅读如下:
fid = fopen('data.txt','r');
data = textscan(fid,'%d','CollectOutput',true);
fclose(fid);
data_inner = data{1,1};
cols = 8; % predefined number of elements per row
rows = numel(data_inner) / cols;
C = reshape(data_inner.',cols,rows).';
输入示例:
1 2 3 4 5 6
7 8
2 3 4 5 6 7
8 9
3 4 5 6 7 8
9 10
输出示例:
C =
1 2 3 4 5 6 7 8
2 3 4 5 6 7 8 9
3 4 5 6 7 8 9 10
完成后,矩阵可以很容易地转换为 table,如下所示:
T = array2table(C)
T =
C1 C2 C3 C4 C5 C6 C7 C8
__ __ __ __ __ __ __ __
1 2 3 4 5 6 7 8
2 3 4 5 6 7 8 9
3 4 5 6 7 8 9 10
我有一个 table 看起来像这样
x x x x x x
x x
y y y y y y
y y
z z z z z z
z z
我想使用 readtable
导入它,这样所有的 x 都在一行中,所有的 y 在下一行中,等等。换句话说,在 .txt 文件中,最后两个内容应该在一行中被转移到下一行。我想我需要更改 DelimitedTextImportOptions
中的某些内容,但我不知道具体是什么。
如果有人能帮助我,我会很高兴,非常感谢!
如果需要使用 readtable,一种选择是将原始文件转换为新格式,然后将 readtable 应用于新文件。
以下是可用于以下示例的文件 in.txt
的示例内容:
1 2 3 abc 5 6
7 8
3 4 5 def 7 8
9 0
9 1 0 ghi 3 2
1 4
代码如下:
% FIRST, TRANSFORM THE INPUT FILE INTO A FILE WHERE THE SPLIT LINES ARE
% COMBINED INTO SINGLE LINES
% open input and output files
in = fopen('in.txt', 'r');
out = fopen('out.txt', 'w');
% read the first line of the input file
currline = fgetl(in);
% while we haven't reached the end of the file
while ~isequal(currline, -1)
% read the following line of the input file
currline_append = fgetl(in);
% ... if it doesn't exist, throw an error; the file is not as expected
if isequal(currline_append, -1)
error('Bad file');
end
% print this pair of lines to the output file as a single line.
% Note: if using Windows Notepad or similar application to read the
% file, you may want to replace '\n' by '\r\n' in the format string
fprintf(out, '%s %s\n', currline, currline_append);
% get the next line of the input file
currline = fgetl(in);
end
% close input and output files
fclose(in);
fclose(out);
% NEXT, READ THE TABLE FROM THE OUTPUT FILE
t = readtable('out.txt');
实际上,如果您的文本文件的形状如您所描述的 EXACTLY 和 STRICTLY (每行具有相同数量的元素,并且其中两个溢出到下一行),你可以很容易地阅读如下:
fid = fopen('data.txt','r');
data = textscan(fid,'%d','CollectOutput',true);
fclose(fid);
data_inner = data{1,1};
cols = 8; % predefined number of elements per row
rows = numel(data_inner) / cols;
C = reshape(data_inner.',cols,rows).';
输入示例:
1 2 3 4 5 6
7 8
2 3 4 5 6 7
8 9
3 4 5 6 7 8
9 10
输出示例:
C =
1 2 3 4 5 6 7 8
2 3 4 5 6 7 8 9
3 4 5 6 7 8 9 10
完成后,矩阵可以很容易地转换为 table,如下所示:
T = array2table(C)
T =
C1 C2 C3 C4 C5 C6 C7 C8
__ __ __ __ __ __ __ __
1 2 3 4 5 6 7 8
2 3 4 5 6 7 8 9
3 4 5 6 7 8 9 10