How to start a time step function? SWITCH expression must be a scalar or a character vector.
Show older comments
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 Comments
Igor Arkhandeev
on 4 Feb 2021
Hello! Can you show full code before switch operation?
Kim Hao Teong
on 4 Feb 2021
Igor Arkhandeev
on 4 Feb 2021
Edited: Igor Arkhandeev
on 4 Feb 2021
Okay, and what input data is?
Igor Arkhandeev
on 4 Feb 2021
I think, your acceleration variable is array.
Answers (2)
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
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.
Categories
Find more on MATLAB in Help Center and File Exchange
Products
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!