MATLAB Answers

Translated by

このページのコンテンツは英語から自動翻訳されています。自動翻訳をオフにする場合は「<a class="turn_off_mt" href="#">ここ</a>」をクリックしてください。

0

nargin doesn't work with my settings

Tom Bedkowski さんによって質問されました 2019 年 1 月 20 日
最新アクティビティ Tom Bedkowski さんによって コメントされました 2019 年 1 月 20 日
Hello,
i got one function to classifiy a Number N.
The classifikation depends on a maximal Number n_max and on the boundaries of the classes KG0,... .
Also it should depend, how much classes i define. So if i define just 4 boundaries KG0-KG3 case 6 should act, because we have 4 boundaries+ 2 standard input numbers N and n_max. If define just 3 boundaries, KG0-KG2 , case 5 should act.
The ClassNumber is ultimately the class in which i put Number N.
Case 5 doesnt work, when i define everything except KG3. Matlab throws: Undefined function or variable 'KG3'. It shall not happen if i go in case 5 isnt it?
Thank you for help.
function [ClassNumber] = Classification(N, n_max, KG0, KG1, KG2, KG3)
switch nargin
case 6
if (KG2<N/n_max) && (N/n_max<=KG3)
ClassNumber=3;
return
elseif (KG1<N/n_max) && (N/n_max<=KG2)
ClassNumber=2;
return
elseif (KG0<=N/n_max) && (N/n_max<=KG1)
ClassNumber=1;
return
end
case 5
if (KG1<N/n_max) && (N/n_max<=KG2)
ClassNumber=2;
return
elseif (KG0<=N/n_max) && (N/n_max<=KG1)
ClassNumber=1;
return
end
end
end

  0 件のコメント

サインイン to comment.

タグ

2 件の回答

回答者: Stephen Cobeldick
2019 年 1 月 20 日
編集済み: Stephen Cobeldick
2019 年 1 月 20 日
 採用された回答

"nargin doesn't work"
nargin does work.
MATLAB input arguments are (for better or for worse) entirely positional. Inputs are not parsed by name, only by their position as inputs. So when you call your function with five input arguments, these are the input arguments that are defined within the function workspace:
function [ClassNumber] = Classification(N, n_max, KG4, KG3, KG2, KG1, KG0)
1, 2, 3, 4, 5 ----------
Clearly with five input arguments KG1 and KG0 are not defined, so any attempt to use them will be an error, exactly as you show in your question. So nargin works perfectly: it counts five input arguments. That you then tell MATLAB to run some code that tries to access variables that you did not define, is not the fault of nargin.

  4 件のコメント

Stephen Cobeldick
2019 年 1 月 20 日
You changed the title to "nargin doesn't work with my settings".
You called your function with five inputs, and this is exactly what nargin counted. I don't see why you think nargin has not "worked": as far as I can tell nargin seems to be working exactly as documented and as expected. There is nothing I can see to justify saying that nargin is not working.
The error you are getting is because you are trying to access variables that have not been defined. You would get exactly the same error like this:
function test()
x
end
Tom Bedkowski 2019 年 1 月 20 日
The order of the class boundaries was wrong. Also KG4 was not relevant. I changed it.
Tom Bedkowski 2019 年 1 月 20 日
Ok i understand.
Thank you :)

サインイン to comment.


回答者: Tom Bedkowski 2019 年 1 月 20 日
編集済み: Tom Bedkowski 2019 年 1 月 20 日

***The problem was, that i defined N, n_max, KG0, KG1, KG2 but not KG3.
***KG3 was not in the workspace. So Matlab throws: Undefined function or variable 'KG3'.
So the function
function [ClassNumber] = Classification(N, n_max, KG0, KG1, KG2, KG3)
doesnt work because Matlab couldnt find KG3.

  0 件のコメント

サインイン to comment.



Translated by