Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/matlab/16.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/377.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 如何修复错误:“引用;A(I):指数超出范围;值1超出范围0“;_Matlab_Octave - Fatal编程技术网

Matlab 如何修复错误:“引用;A(I):指数超出范围;值1超出范围0“;

Matlab 如何修复错误:“引用;A(I):指数超出范围;值1超出范围0“;,matlab,octave,Matlab,Octave,所以我做了这个函数,但我不知道为什么会出现这个错误。我怎样才能修好它 错误:LOADC:A(I):索引超出范围;值1超出范围0 错误:从调用 第15行第13列处的荷载C 直接原因是您试图访问空数组(其中没有值)中的值 发生这种情况的原因是,您在第一个循环中读取整个文件,而循环将文件指针放在文件的末尾。然后(在不重置文件指针的情况下),尝试在for循环中逐行读取它。由于文件指针已经位于文件的末尾,fgetl将始终返回一个空数组([]),因此,当您开始尝试使用它时,会出现显示的索引错误 解决方案是两

所以我做了这个函数,但我不知道为什么会出现这个错误。我怎样才能修好它

错误:LOADC:A(I):索引超出范围;值1超出范围0

错误:从调用 第15行第13列处的荷载C


直接原因是您试图访问空数组(其中没有值)中的值

发生这种情况的原因是,您在第一个
循环中读取整个文件,而
循环将文件指针放在文件的末尾。然后(在不重置文件指针的情况下),尝试在
for
循环中逐行读取它。由于文件指针已经位于文件的末尾,
fgetl
将始终返回一个空数组(
[]
),因此,当您开始尝试使用它时,会出现显示的索引错误

解决方案是两个选项之一:

  • for
    循环之前调用
    frewind(ficheiro)
    ,重置指向文件开头的文件指针,以便成功读取每一行

  • 想出一种更好的解析文件的方法,而不是仅仅为了计算文件中的行数而在整个文件中循环

  • fid = fopen('filename.txt', 'rt');
    lines = textscan(fid, '%s', 'HeaderLines', 1);
    fclose(fid);
    
    % For each line we want to get the reference off of the front
    parts = regexp(lines{1}, ';', 'split');
    
    materials = cell(1, numel(parts));
    elements = cell(1, numel(parts));
    percentages = cell(1, numel(parts));
    
    for k = 1:numel(parts)
        % Remove the last empty blank
        parts{k}(end) = [];
    
        % Get the material (the first element)
        materials{k} = parts{k}{1};
    
        % Get the elements
        elements{k} = parts{k}(2:2:end);
    
        % Get the percents
        percents{k} = parts{k}(3:2:end);
    end
    
    如果您发布一些文件内容,我们可能会为您提供一种更好的方法,用一两行代码解析文件

    更新

    另外,如果你看这一行,
    n
    一直到
    pontovirgula
    的末尾

    for n = 1:2:length(pontovirgula)
    
    但是,然后您尝试通过数组末尾访问
    1
    2

    pontovirgula(n+1)
    pontovirgula(n+2)
    
    这肯定会引起问题。尝试只循环直到距离末端2

    for n = 1:2:(numel(pontovirgula) - 2)
    
    更新2

    鉴于您已经发布了文件内容,下面是另一种解析文件的方法

    fid = fopen('filename.txt', 'rt');
    lines = textscan(fid, '%s', 'HeaderLines', 1);
    fclose(fid);
    
    % For each line we want to get the reference off of the front
    parts = regexp(lines{1}, ';', 'split');
    
    materials = cell(1, numel(parts));
    elements = cell(1, numel(parts));
    percentages = cell(1, numel(parts));
    
    for k = 1:numel(parts)
        % Remove the last empty blank
        parts{k}(end) = [];
    
        % Get the material (the first element)
        materials{k} = parts{k}{1};
    
        % Get the elements
        elements{k} = parts{k}(2:2:end);
    
        % Get the percents
        percents{k} = parts{k}(3:2:end);
    end
    

    直接原因是您试图访问空数组(其中没有值)中的值

    发生这种情况的原因是,您在第一个
    循环中读取整个文件,而
    循环将文件指针放在文件的末尾。然后(在不重置文件指针的情况下),尝试在
    for
    循环中逐行读取它。由于文件指针已经位于文件的末尾,
    fgetl
    将始终返回一个空数组(
    []
    ),因此,当您开始尝试使用它时,会出现显示的索引错误

    解决方案是两个选项之一:

  • for
    循环之前调用
    frewind(ficheiro)
    ,重置指向文件开头的文件指针,以便成功读取每一行

  • 想出一种更好的解析文件的方法,而不是仅仅为了计算文件中的行数而在整个文件中循环

  • fid = fopen('filename.txt', 'rt');
    lines = textscan(fid, '%s', 'HeaderLines', 1);
    fclose(fid);
    
    % For each line we want to get the reference off of the front
    parts = regexp(lines{1}, ';', 'split');
    
    materials = cell(1, numel(parts));
    elements = cell(1, numel(parts));
    percentages = cell(1, numel(parts));
    
    for k = 1:numel(parts)
        % Remove the last empty blank
        parts{k}(end) = [];
    
        % Get the material (the first element)
        materials{k} = parts{k}{1};
    
        % Get the elements
        elements{k} = parts{k}(2:2:end);
    
        % Get the percents
        percents{k} = parts{k}(3:2:end);
    end
    
    如果您发布一些文件内容,我们可能会为您提供一种更好的方法,用一两行代码解析文件

    更新

    另外,如果你看这一行,
    n
    一直到
    pontovirgula
    的末尾

    for n = 1:2:length(pontovirgula)
    
    但是,然后您尝试通过数组末尾访问
    1
    2

    pontovirgula(n+1)
    pontovirgula(n+2)
    
    这肯定会引起问题。尝试只循环直到距离末端2

    for n = 1:2:(numel(pontovirgula) - 2)
    
    更新2

    鉴于您已经发布了文件内容,下面是另一种解析文件的方法

    fid = fopen('filename.txt', 'rt');
    lines = textscan(fid, '%s', 'HeaderLines', 1);
    fclose(fid);
    
    % For each line we want to get the reference off of the front
    parts = regexp(lines{1}, ';', 'split');
    
    materials = cell(1, numel(parts));
    elements = cell(1, numel(parts));
    percentages = cell(1, numel(parts));
    
    for k = 1:numel(parts)
        % Remove the last empty blank
        parts{k}(end) = [];
    
        % Get the material (the first element)
        materials{k} = parts{k}{1};
    
        % Get the elements
        elements{k} = parts{k}(2:2:end);
    
        % Get the percents
        percents{k} = parts{k}(3:2:end);
    end
    

    检查linha的长度,以及用于索引linha的pontovirgula的值。

    检查linha的长度,以及用于索引linha的pontovirgula的值。

    感谢您的帮助,我尝试呼叫frewind(ficheiro),但现在它给我错误:LOADC:A(i):索引超出范围;值6越界5错误:从LOADC第18行第14列的ElemX=linha处调用(pontovirgula(n)+1:pontovirgula(n+1)-1)@VascoMartins查看我的更新。此外,我强烈建议您发布您的实际文件内容,以便我们可以向您展示更好的方式。这是非常低效的。我还更改了您前面提到的for循环,它不再给我错误。
    这应该是在数据库中插入数据,您知道如何打开数据库并查看数据是否正确吗?@VascoMartins这是另一个问题。我发布了另一个更新,它解析了所有内容。我感谢你的帮助,但我认为我不能使用你在更新2中写的内容。这对我在课堂上学到的东西来说太高级了。谢谢你的帮助,我试着给frewind(ficheiro)打电话,但现在它给了我错误:LOADC:A(i):索引越界;值6越界5错误:从LOADC第18行第14列的ElemX=linha处调用(pontovirgula(n)+1:pontovirgula(n+1)-1)@VascoMartins查看我的更新。此外,我强烈建议您发布您的实际文件内容,以便我们可以向您展示更好的方式。这是非常低效的。我还更改了您前面提到的for循环,它不再给我错误。
    这应该是在数据库中插入数据,您知道如何打开数据库并查看数据是否正确吗?@VascoMartins这是另一个问题。我发布了另一个更新,它解析了所有内容。我感谢你的帮助,但我认为我不能使用你在更新2中写的内容。这对我在课堂上所学的东西来说太高级了。