Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/matlab/15.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何在matlab中读取文本文件中一定长度的数据并存储在整数数组中_Matlab_Textscan - Fatal编程技术网

如何在matlab中读取文本文件中一定长度的数据并存储在整数数组中

如何在matlab中读取文本文件中一定长度的数据并存储在整数数组中,matlab,textscan,Matlab,Textscan,我有一个如下所示的文本文件: 777 3 1000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000010000000000000000000000000

我有一个如下所示的文本文件:

777
3
100000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000010000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000100000000000000000000000000000000000000000000000000000000000000000000010000000000000000000000000000000000000000000000000000000
010000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000100000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000100000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000100000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000010000000000000000000000000000000000000000000000000000000000000000000001000000000000000000000000000000000000000000000000000000
001000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000010000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000010000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000100000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000010000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001000000000000000000000000000000000000000000000000000000000000000000000100000000000000000000000000000000000000000000000000000
H = [
1 0 0 0 ...
0 1 0 0 ...
0 0 1 0 ...
]
前两行是数据的维度,对于这个特定示例,有3行数据和777列(或每行中的值)

我需要将这些数据读入Matlab,并将结果存储为整数数组。 即,我的数组是3行x 777列,如下所示:

777
3
100000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000010000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000100000000000000000000000000000000000000000000000000000000000000000000010000000000000000000000000000000000000000000000000000000
010000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000100000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000100000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000100000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000010000000000000000000000000000000000000000000000000000000000000000000001000000000000000000000000000000000000000000000000000000
001000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000010000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000010000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000100000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000010000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001000000000000000000000000000000000000000000000000000000000000000000000100000000000000000000000000000000000000000000000000000
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);
我将使用、、和两个循环来设计一段适合您特定问题的代码:

% 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
    }
}
string str=”“;
字符串str2=“”;//存储的加载地址。
//myfile.open(argv[1]);//打开文件
ifstream read1;//名为read1的iostream变量
文件1.打开(argv[1]);
//内存将通过读取程序文件来初始化自身。
库特