dot indexing is not supported for variables of this type
185 次查看(过去 30 天)
显示 更早的评论
CODE:
function F = system_of_equations(x)
global def;
global m;
global init;
global P_alpha_1;
s=def.k ;
C1=x(1,1:s);
C2=x(1,(s+1):2*s);
x1=(C1*P_alpha_1*m.H) + init(1);
u=(C2*m.H) ;
D_alpha1_x1=C1*m.H;
%
M=Haar_matrix(s);
HC=M(:,s);
%%control law1
F = horzcat( D_alpha1_x1 + x1 - u , ...
(C1*P_alpha_1*HC) + init(1) - (1/2*((1-exp(-1))^2))) ;
end
I am getting an error as
Dot indexing is not supported for variables of this type.
Error in system_of_equations (line 10)
s=def.k
I am running the function as system_of_equations(zeros(1,16)) and getting the above error.
How to solve this problem?
2 个评论
回答(2 个)
Walter Roberson
2022-2-18
The global variable def was not initialized, so it has the default value that global variables have, 0x0 double precision array. Double precision arrays cannot be dot-indexed.
Moral of the story is to avoid using global variables.
0 个评论
Bjorn Gustavsson
2022-2-18
Try your hardest to avoid global variables. They typically lead to bugs and errors that are hard to understand and trace - because they can be caused by variable-modifications far from the function where the errors appear. In this case use additional input-variables to your function:
function F = system_of_equations(x,def, m, init, P_alpha_1)
% global def;
% global m;
% global init;
%
% global P_alpha_1;
s=def.k ;
C1=x(1,1:s);
C2=x(1,(s+1):2*s);
x1=(C1*P_alpha_1*m.H) + init(1);
u=(C2*m.H) ;
D_alpha1_x1=C1*m.H;
%
M=Haar_matrix(s);
HC=M(:,s);
%%control law1
F = horzcat( D_alpha1_x1 + x1 - u , ...
(C1*P_alpha_1*HC) + init(1) - (1/2*((1-exp(-1))^2))) ;
end
Then you will know what variables you send in to the function and can be sure of what they represent.
In your case you might have called another function that also defined a global variable def and then assigned some value to that, for example any of these:
def = pi^.5;
def = 'my favourite global variable';
def = {exp(1i*pi*(sqrt(5)+1)/2),'well'};
and then your carefully designed def will be poofed out of existens.
Try your very hardest to avoid global variables.
0 个评论
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Matrix Indexing 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!