Help me in debugging this code. Error : Unable to perform assignment because the left and right sides have a different number of elements.

4 vues (au cours des 30 derniers jours)
clc;
clear all;
close all;
global th
% Define joint angles
th = struct( ...
'th1', [0; 0; 0], ...
'th2', [0; pi/4; 0], ...
'th3', [0; pi/4; 0] ...
);
i = 1;
while (1)
human(th);
pause(0.1);
i = i + 1;
move1();
cla;
end
th1_1: 0 th2_2: 0.7854
Unable to perform assignment because the left and right sides have a different number of elements.

Error in solution>move1 (line 39)
th.th2(2) = pi/4 + (pi/2) * index1;
%% Motion function
function move1()
global th i
k = [1:1:10 9:-1:2];
N1 = length(k);
if i > N1
i = i - N1;
elseif i < 1
i = i + N1;
end
% Interpolation index
index1 = (k(i) - 1) / 9;
% ERROR MAY OCCUR HERE
th.th2(2) = pi/4 + (pi/2) * index1;
th.th3(2) = pi/4 - (pi/2) * index1;
end
%% Human visualization function
function human(th)
% Accessing angles
th1_1 = th.th1(1);
th2_2 = th.th2(2);
% ERROR MAY OCCUR IF STRUCTURE INDEXING IS WRONG
disp(['th1_1: ', num2str(th1_1)]);
disp(['th2_2: ', num2str(th2_2)]);
end
So i am trying to make a humanoid moving. So for that i am modifying the theta(th) and i am getting this error but both the LHS and RHS is scalar. Still it is showing this error. Any ideas will help....

Réponse acceptée

Cris LaPierre
Cris LaPierre le 13 Mar 2025
You need to also declare i as global in your script.
  1 commentaire
Walter Roberson
Walter Roberson le 13 Mar 2025
That is to say, in your script you just assign to i without having declared it global.
Declaring something as global only affects the environments in which it is declared as global, not all environments with the same variable name.
If it were otherwise, then you would be able to affect security or banking functions by declaring global variables with the same name as the local variables within the function, which is obviously undesireable.

Connectez-vous pour commenter.

Plus de réponses (0)

Catégories

En savoir plus sur Resizing and Reshaping Matrices dans Help Center et File Exchange

Produits


Version

R2024b

Community Treasure Hunt

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

Start Hunting!

Translated by