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

5 ビュー (過去 30 日間)
Kim Hao Teong
Kim Hao Teong 2021 年 2 月 4 日
編集済み: Jan 2021 年 2 月 5 日
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 件のコメント
Igor Arkhandeev
Igor Arkhandeev 2021 年 2 月 4 日
編集済み: Igor Arkhandeev 2021 年 2 月 4 日
Okay, and what input data is?
Igor Arkhandeev
Igor Arkhandeev 2021 年 2 月 4 日
I think, your acceleration variable is array.

サインインしてコメントする。

回答 (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 Center および File ExchangeMATLAB についてさらに検索

製品

Community Treasure Hunt

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

Start Hunting!

Translated by