dot indexing is not supported for variables of this type
137 ビュー (過去 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 件のコメント
KSSV
2022 年 2 月 18 日
What is
class(def)
You are trying to access it like a structure. Is it a structure?
回答 (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 で Creating and Concatenating Matrices についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!