SWITCH expression must be a scalar or a character vector. Error in pathplaninglaila (line 17) switch mode
조회 수: 73 (최근 30일)
이전 댓글 표시
% Parameters
spacing = 10; % spiral spacing
delta_theta = pi/4; % spiral angle increment
pass_width = 10; % S-path pass width
% Initialize position
x = 0;
y = 0;
% Initialize path mode
mode = 'random';
% Main loop
while true
% Get next position based on mode
switch mode
case 'random'
dx = rand*10 - 5;
dy = rand*10 - 5;
case 'spiral'
n = n+1;
r = n*spacing;
theta = n*delta_theta;
dx = r*cos(theta);
dy = r*sin(theta);
case 's-path'
if mod(y, pass_width)==0
dy = pass_width;
else
dy = -pass_width;
end
case 'wall follow'
% Check sensors and turn if needed
% ...
dx = 1;
end
% Update position
x = x + dx;
y = y + dy;
% Check for obstacles
if obstacleDetected(x,y)
% Backup and change path
x = x - dx;
y = y - dy;
mode = selectNewMode();
end
% Send motion commands
moveTo(x,y);
% Randomly change mode
if rand < 0.1
mode = selectNewMode();
end
end
% Helper functions
function out = obstacleDetected(~,~)
% Check map for obstacle at x,y
out = false;
end
function m = selectNewMode()
% Pick random mode
m = randsample({'random','spiral','s-path','wall follow'},1);
end
function moveTo(x,y)
% Send motion commands
disp(['Moving to ' num2str(x) ',' num2str(y)]);
end
댓글 수: 0
답변 (2개)
Dyuman Joshi
2023년 12월 3일
As the error states, the switch expression is expected to be a scalar or character vector. In your case, you are dealing with text data, thus it should be a character vector.
Modify your function selectNewMode to provide the output as a character vector -
function m = selectNewMode()
% Pick random mode
m = randsample({'random','spiral','s-path','wall follow'},1);
m = m{:};
end
Note that you have not defined the variable n, to use it in the spiral case.
Also you have not put a break condition for the while loop.
And the function obstacleDetected paired with the if-else condition seems redundant.
댓글 수: 0
VBBV
2023년 12월 10일
The mode variable used for the switch expression needs a character data type. you need to convert that variable to char mode as shown below. Some variables inside the switch-case statement are not defined and the while loop seems to run endlessly. Use a suitable while loop condition to stop the loop or break statements inside the loop.
% Parameters
spacing = 10; % spiral spacing
delta_theta = pi/4; % spiral angle increment
pass_width = 10; % S-path pass width
% Initialize position
x = 0;
y = 0;
% Initialize path mode
mode = 'random';
n = 0; % initialize n
% Main loop
while true
% Get next position based on mode
switch mode
case 'random'
dx = rand*10 - 5;
dy = rand*10 - 5;
case 'spiral'
n = n+1;
r = n*spacing;
theta = n*delta_theta;
dx = r*cos(theta);
dy = r*sin(theta);
case 's-path'
if mod(y, pass_width)==0
dy = pass_width;
else
dy = -pass_width;
end
case 'wall follow'
% Check sensors and turn if needed
% ...
dx = 1;
end
% Update position
x = x + dx;
y = y + dy;
% Check for obstacles
if obstacleDetected(x,y)
% Backup and change path
x = x - dx;
y = y - dy;
mode = selectNewMode();
mode = char(mode); % convert it to char
end
% Send motion commands
moveTo(x,y);
% Randomly change mode
if rand < 0.1
mode = selectNewMode();
mode = char(mode); % convert to char
end
end
% Helper functions
function out = obstacleDetected(~,~)
% Check map for obstacle at x,y
out = false;
end
function m = selectNewMode()
% Pick random mode
m = randsample({'random','spiral','s-path','wall follow'},1);
end
function moveTo(x,y)
% Send motion commands
disp(['Moving to ' num2str(x) ',' num2str(y)]);
end
댓글 수: 0
참고 항목
카테고리
Help Center 및 File Exchange에서 Develop Apps Using App Designer에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!