How to start a time step function? SWITCH expression must be a scalar or a character vector.

5 次查看(过去 30 天)
I got this code from this community. This is the code for Newmark's method. Note that I only included part of the script.
switch acceleration
case 'Average'
gaama = 1/2 ;beta = 1/4 ;
case 'Linear'
gaama = 1/2 ;beta = 1/6 ;
end
.......
% Time step starts
for i = 1:nt
delP = P0+a*vel(:,i)+b*accl(:,i) ;
delu = Kcap\delP ;
delv = a1*delu-a4*vel(:,i)-a6*accl(:,i) ;
dela = a2*delu-a3*vel(:,i)-a5*accl(:,i);
disp(:,i+1) = disp(:,i)+delu ;
vel(:,i+1) = vel(:,i)+delv ;
accl(:,i+1) = accl(:,i)+dela ;
U(:,i+1) = phi*disp(:,i+1) ;
end
I loaded in a txt. file (which is an acceleration data, the matrix size is 2674x1) into the workspace, and then I ran the code. I don't know what went wrong it says: SWITCH expression must be a scalar or a character vector.
  4 个评论

请先登录,再进行评论。

回答(2 个)

Adam Danz
Adam Danz 2021-2-4
编辑:Adam Danz 2021-2-5
Take a moment to read the switch/case documentation to become familiar with what that function does.
The acceleration variable needs to match either 'Average' or 'Linear' so it must be a character vector or string. That's case sensitive, too!
To avoid problems with case sensitivity,
switch lower(acceleration) % set to lower case
case 'average' % lower case!
gaama = ...
case 'linear' % lower case!
gaama = ...
end
Lastly, I urse people to include an "otherwise" to catch these types of errors. The "otherwise" section can either throw an error or set a default value.
switch lower(acceleration) % set to lower case
case 'average' % lower case!
gaama = ...
case 'linear' % lower case!
gaama = ...
otherwise
error('No case match for acceleration input.')
% or
% gaama = ...
end

Jan
Jan 2021-2-5
编辑:Jan 2021-2-5
SWITCH expression must be a scalar or a character vector.
The error message tells you, that the 7th input of the function is neither a CHAR vector nor a scalar. So how did you define acceleration in the caller?
NewmarkMethod(M,K,C,P,phi,dof,acceleration)
The method to find the cause of such error is to use the debugger. Type this in the command window:
dbstop if error
Then run your code again. Matlab stops at the error and you cancheck the contents of the concerned variable.

类别

Help CenterFile Exchange 中查找有关 MATLAB 的更多信息

产品

Community Treasure Hunt

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

Start Hunting!

Translated by