matlab中如何读取文本文件中一定长度的数据并存入整数数组
How to read data of a certain length in text file and store in an integer array in matlab
我有一个如下所示的文本文件:
777
3
100000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000010000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000100000000000000000000000000000000000000000000000000000000000000000000010000000000000000000000000000000000000000000000000000000
010000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000100000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000100000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000100000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000010000000000000000000000000000000000000000000000000000000000000000000001000000000000000000000000000000000000000000000000000000
001000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000010000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000010000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000100000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000010000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001000000000000000000000000000000000000000000000000000000000000000000000100000000000000000000000000000000000000000000000000000
前两行是数据的维度,对于此特定示例,有 3 行数据和 777 列(或每行中的值)。
我需要将此数据读入 Matlab 并将结果存储为整数数组。
IE。我的数组是 3 行 x 777 列,看起来像这样:
H = [
1 0 0 0 ...
0 1 0 0 ...
0 0 1 0 ...
]
我在使用特定维度读取数据以及从文件中选取前两个值时遇到问题。我所做的只是尝试读取数据而不是尺寸只是删除了前两行,但我宁愿不这样做。我在下面粘贴了我尝试过的代码,我尝试了两种不同的方法但没有得到想要的结果:
% Method 1
H = textread('myTextFile.txt', '%s');
ncols = size(H, 1);
nrows = size(H{1}, 2);
H = reshape(sscanf([H{:}], '%1d'), ncols,nrows);
% Method 2
fid = fopen('myTextFile.txt', 'r');
H = textscan(fid,'%777s');
fclose(fid);
我会使用 fopen
, feof
, fgetl
, str2double
和两个循环来设计一段适合您特定问题的代码:
% Open file.
fid = fopen('myTextFile.txt', 'r');
% Initialize row index for H.
a = 1;
% Initialize number of line counter for file.
nline = 1;
% Test for end-of-file.
while ~feof(fid)
% Read line from file as string.
line = fgetl(fid);
% Test for number of line geater than 2.
if(nline > 2)
% Loop through every character from the string.
% b is column index for H.
for b = 1:length(line)
% Extract char from the string, convert it to double and store it in H.
H(a, b) = str2double(line(b));
end
% Increase row index for H.
a = a + 1;
end
% Increase number of line counter.
nline = nline + 1;
end
% Close file.
fclose(fid);
这是我编写的一些代码,它从文件中读取行并将它们存储到整数数组中:
string str = "";
string str2 = ""; // To store load address for .
// myfile.open(argv[1]); // opening file
ifstream read1; // iostream variable named read1
file1.open(argv[1]);
// Memory will initialize itself by reading a program file.
cout << "File name: " << argv[1] << endl;
if(file1.is_open()) {
cout << "File Open!" << endl;
}
while(file1.good()) // Read till end of file
{
getline(file1, str); // Read string
//cout << str << endl;
if(isdigit(str[0]))
{
number[c++] = atoi(str.c_str()); // write(address, data) - writes
// the data to the address
}
}
我有一个如下所示的文本文件:
777
3
100000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000010000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000100000000000000000000000000000000000000000000000000000000000000000000010000000000000000000000000000000000000000000000000000000
010000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000100000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000100000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000100000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000010000000000000000000000000000000000000000000000000000000000000000000001000000000000000000000000000000000000000000000000000000
001000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000010000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000010000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000100000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000010000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001000000000000000000000000000000000000000000000000000000000000000000000100000000000000000000000000000000000000000000000000000
前两行是数据的维度,对于此特定示例,有 3 行数据和 777 列(或每行中的值)。
我需要将此数据读入 Matlab 并将结果存储为整数数组。 IE。我的数组是 3 行 x 777 列,看起来像这样:
H = [
1 0 0 0 ...
0 1 0 0 ...
0 0 1 0 ...
]
我在使用特定维度读取数据以及从文件中选取前两个值时遇到问题。我所做的只是尝试读取数据而不是尺寸只是删除了前两行,但我宁愿不这样做。我在下面粘贴了我尝试过的代码,我尝试了两种不同的方法但没有得到想要的结果:
% Method 1
H = textread('myTextFile.txt', '%s');
ncols = size(H, 1);
nrows = size(H{1}, 2);
H = reshape(sscanf([H{:}], '%1d'), ncols,nrows);
% Method 2
fid = fopen('myTextFile.txt', 'r');
H = textscan(fid,'%777s');
fclose(fid);
我会使用 fopen
, feof
, fgetl
, str2double
和两个循环来设计一段适合您特定问题的代码:
% Open file.
fid = fopen('myTextFile.txt', 'r');
% Initialize row index for H.
a = 1;
% Initialize number of line counter for file.
nline = 1;
% Test for end-of-file.
while ~feof(fid)
% Read line from file as string.
line = fgetl(fid);
% Test for number of line geater than 2.
if(nline > 2)
% Loop through every character from the string.
% b is column index for H.
for b = 1:length(line)
% Extract char from the string, convert it to double and store it in H.
H(a, b) = str2double(line(b));
end
% Increase row index for H.
a = a + 1;
end
% Increase number of line counter.
nline = nline + 1;
end
% Close file.
fclose(fid);
这是我编写的一些代码,它从文件中读取行并将它们存储到整数数组中:
string str = "";
string str2 = ""; // To store load address for .
// myfile.open(argv[1]); // opening file
ifstream read1; // iostream variable named read1
file1.open(argv[1]);
// Memory will initialize itself by reading a program file.
cout << "File name: " << argv[1] << endl;
if(file1.is_open()) {
cout << "File Open!" << endl;
}
while(file1.good()) // Read till end of file
{
getline(file1, str); // Read string
//cout << str << endl;
if(isdigit(str[0]))
{
number[c++] = atoi(str.c_str()); // write(address, data) - writes
// the data to the address
}
}