problem in calling a function, it does not recognize "switch case"

Please suggest the solution for the following problem. I am doing this in MATLAB 2021a. Thanks
I have three functions (1). function DSSK_Cons_Diagram = DSSK_Cons_Diagram_Gen(Nt )
(2). function DSM_Cons_Diagram=DSM_Cons_Diagram_Gen(Nt ,M)
(3). function DSMTConstellation_Mat = DSMT_Cons_Diagram_Gen(DSMT_SyS ,Nt ,M)
(1) and (2) works properly when I call them separately in the command window but the (3) has a problem it always says "unknown system". To call (3), I am writing like this,
DSMT_SyS =["dssk", "dsm"]; %DSMT_SyS is A string indicating the DSMT system for :
% −’DSSK’ for differential space shift keying ;
% − DSM’ for differential spatial modulation .
Nt=2,M=2;
DSMT_Cons_Diagram_Gen(DSMT_SyS ,Nt ,M)
The (3) is written here,
function DSMTConstellation_Mat = DSMT_Cons_Diagram_Gen(DSMT_SyS ,Nt ,M)
switch lower(DSMT_SyS)
case 'dssk'
DSMTConstellation_Mat = DSSK_Cons_Diagram_Gen(Nt ) ;
case 'dsm'
DSMTConstellation_Mat = DSM_Cons_Diagram_Gen(Nt ,M) ;
otherwise
error('Unknown system' )
end
end

8 件のコメント

Stephen23
Stephen23 2022 年 7 月 11 日
You cannot supply SWITCH with a non-scalar string array as you are doing, you must provide it with a scalar string (or a character vector). You could use a loop, ARRAYFUN, or CELLFUN.
Dyuman Joshi
Dyuman Joshi 2022 年 7 月 11 日
編集済み: Dyuman Joshi 2022 年 7 月 11 日
1 - Your input is string and the comparison is done with char (switch statement)
2 - Your input is an array while the comparison is done for a (singular) element
EDIT - My first statement is incorrect, as showed by Stephen below.
Stephen23
Stephen23 2022 年 7 月 11 日
編集済み: Stephen23 2022 年 7 月 11 日
@Dyuman Joshi: the SWITCH operator accepts STRING objects (and all objects that have an EQ method):
str = "hello"; % string
switch str
case 'hello'
disp world
otherwise
disp failed
end
world
The problem is that the OP provides a non-scalar string array.
Dyuman Joshi
Dyuman Joshi 2022 年 7 月 11 日
@Stephen23, I was not aware of this. Thanks for the info. I will edit my comment accordingly.
Also, If you don't mind me asking, What do you mean by EQ method?
Sajid Sarwar
Sajid Sarwar 2022 年 7 月 11 日
Stephen23 Thanks for the answer. I got it.
"What do you mean by EQ method?"
methods('string')
Methods for class string: append contains eq extractAfter gt issorted lt pad reverse startsWith cellstr count erase extractBefore insertAfter join matches plus sort strip char double eraseBetween extractBetween insertBefore le ne replace split strlength compose endsWith extract ge ismissing lower or replaceBetween splitlines upper
Dyuman Joshi
Dyuman Joshi 2022 年 7 月 11 日
Oh, okay, eq is short for equality, alright. Thanks for the info, it's quite helpful!
Steven Lord
Steven Lord 2022 年 7 月 11 日
Most of the operators have a function associated with them that you can overload to overload that operator (for example, plus is the function you overload to customize how your class works with the + operator.) See the table on this documentation page for more information about the operators and their corresponding function forms.

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

回答 (0 件)

カテゴリ

ヘルプ センター および File ExchangeData Type Conversion についてさらに検索

質問済み:

2022 年 7 月 11 日

コメント済み:

2022 年 7 月 11 日

Community Treasure Hunt

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

Start Hunting!

Translated by