我有一个看起来像这样的文本文件:
777
3
100000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000010000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000100000000000000000000000000000000000000000000000000000000000000000000010000000000000000000000000000000000000000000000000000000
010000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000100000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000100000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000100000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000010000000000000000000000000000000000000000000000000000000000000000000001000000000000000000000000000000000000000000000000000000
001000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000010000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000010000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000100000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000010000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001000000000000000000000000000000000000000000000000000000000000000000000100000000000000000000000000000000000000000000000000000
前两行是数据的维度,对于此特定示例,有3行数据和777列(或每行中的值)。
我需要将这些数据读入Matlab并将结果存储为整数数组。 即。我的数组将是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);
答案 0 :(得分:1)
我会使用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);
答案 1 :(得分:0)
这是我创建的一些代码,它从文件中读取行并将它们存储到整数数组中:
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
}
}