필터 지우기
필터 지우기

dot indexing is not supported for variables of this type

조회 수: 166 (최근 30일)
Kunal Jain
Kunal Jain 2022년 2월 18일
답변: Bjorn Gustavsson 2022년 2월 18일
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
KSSV 2022년 2월 18일
What is
class(def)
You are trying to access it like a structure. Is it a structure?
Kunal Jain
Kunal Jain 2022년 2월 18일
Thanks for answering my question.
By using s=def.k , I have triying to define a matrix C1 and and C2 as given.
C1=x(1,1:s);
C2=x(1,(s+1):2*s);
Can you tell how is it coming as a structure and what should be the changes?
Thanks in advance

댓글을 달려면 로그인하십시오.

답변 (2개)

Walter Roberson
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.

Bjorn Gustavsson
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.

카테고리

Help CenterFile Exchange에서 Creating and Concatenating Matrices에 대해 자세히 알아보기

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by