-1

我正在尝试使用 MatLAB 从 m*n 数据表中选择一个特定的数组。
错误消息:索引超出矩阵维度

检查以下 MatLAB 脚本:

    t_res = 0.2;
    c_angle = 360/30;
    iact = 0.1;
    V = 12;
    p = 1;
    
    for p=1:length(p),
    for p=1:5,
        Time = p*t_res;
        Angle = Time*c_angle/2;
        if (p == 1)
            delt_i = 0; 
        else
            delt_i = 0.5* t_res* V /L_obt;
        end
     iact = iact+delt_i;
    
     [~, ~, raw] = xlsread('F:\User\Matlab\data1.xlsx','Sheet3','A2:D11');
     data = reshape([raw{:}],size(raw)); 
     Current = data(:,1);
     Angle1 = data(:,2);
     Torque = data(:,3);
     Fluxlinkage = data(:,4);
     
    
     F = scatteredInterpolant(Current,Angle1,Fluxlinkage);
     Fluxlinkage = F(iact,Angle);
     L_obt = Fluxlinkage/iact; 
     F = scatteredInterpolant(Current,Angle1,Torque);
     Torque = F(iact,Angle);
    
    
    Table = [p' Time' Angle' iact' delt_i' abs(Torque)' Fluxlinkage' L_obt'];
    fprintf('%d\t\t %f\t\t %f\t\t %f\t\t %f\t\t %f\t\t %f\t\t %f\n', Table');
    
    end
    p=p+1;
    Table(3,5);
    end

收到错误消息:尝试访问 Table(3,5);索引超出范围,因为 size(Table)=[1,8]

4

2 回答 2

1

首先,在第 11 行你说:

delt_i = 0.5* t_res* V /L_obt;

L_obt似乎没有定义。

然后,您对第 38 行的指令有疑问:

Table(3,5);

由于Table是一维数组(一行 8 个值),因此您只需要一个介于 1 和 8 之间的数字,例如Table(3)or Table(5)。在二维数组的情况下,Table(3,5)这意味着您需要第 3 行和第 5 列中的值。

于 2017-10-24T12:47:42.533 回答
1

这是您的代码的重组版本。我将不需要在循环中的东西移出它。并对你真正想做的事情做了一些假设。有关更改的说明,请参阅下面的评论。注意:我无法测试这个,因为我没有你的电子表格,但它至少应该让你更接近。

t_res = 0.2;
c_angle = 360/30;
iact = 0.1;
V = 12;
%p = 1;  %This does nothing since your iterate over p in the loop

%These things don't depend n "p" so don't put them in the loop
[~, ~, raw] = xlsread('F:\Pradeep\Matlab\data1.xlsx','Sheet3','A2:D11');
data = reshape([raw{:}],size(raw));
Current = data(:,1);
Angle1 = data(:,2);
Torque = data(:,3);
Fluxlinkage = data(:,4);
F1 = scatteredInterpolant(Current,Angle1,Fluxlinkage);
F2 = scatteredInterpolant(Current,Angle1,Torque);

%I assume you want to build your var "Table" in the loop ... not to be
%confused with an actaul Matlab type "table" ... so pre-allocate
Table = zeros(5,8);

for p=1:5,
    Time = p*t_res;
    Angle = Time*c_angle/2;
    if (p == 1)
        delt_i = 0;
    else
        delt_i = 0.5* t_res* V /L_obt;   
    end
    iact = iact+delt_i;

    Fluxlinkage = F1(iact,Angle);
    L_obt = Fluxlinkage/iact;
    Torque = F2(iact,Angle);    

    %You were just overwritning the same row again and again ... so create
    %a new row for each results.
    Table(p,:) = [p Time Angle iact delt_i abs(Torque) Fluxlinkage L_obt];
    %Print the new row.
    fprintf('%d\t\t %f\t\t %f\t\t %f\t\t %f\t\t %f\t\t %f\t\t %f\n', Table(p,:));

end
%Not sure what this is for ... since the output is surpressed by the
%semi-colon ???
Table(3,5);
于 2017-10-24T12:56:00.700 回答