Not enough input arguments? Why does this not work?

13 ビュー (過去 30 日間)
Tab for Lab
Tab for Lab 2015 年 10 月 4 日
編集済み: Stephen23 2015 年 10 月 4 日
I have this function:
function [class_type] = valclass(num, class_res)
a = (num)
b = (class_res)
class_type = b(a)
end
Why does it not work?
I am basically saying this:
char(2)
single(2)
int32(2)
double(2)
and i want it to convert respectively but its not doing it. Instead i get an error message saying not enough inputs?

回答 (1 件)

Stephen23
Stephen23 2015 年 10 月 4 日
編集済み: Stephen23 2015 年 10 月 4 日
You don't need to write a function, just use the inbuilt cast function:
>> X = 1.234
X =
1.234
>> class(X)
ans =
double
>> Y = cast(X,'int64')
Y =
1
>> class(Y)
ans =
int64
>> Z = cast(X,'single')
Z =
1.234
>> class(Z)
ans =
single
OR you could use function handles:
>> F = @single
F =
@single
>> A = F(X)
A =
1.234
>> class(A)
ans =
single
  2 件のコメント
Tab for Lab
Tab for Lab 2015 年 10 月 4 日
Thank You so much, but i think i can still incorporate it into a function by doing:
cast(num, class_res) am I right?
Stephen23
Stephen23 2015 年 10 月 4 日
編集済み: Stephen23 2015 年 10 月 4 日
Of course you can include it in another function, if you want to.
But considering the function that you defined in your original question there is no point, as they would be doing exactly the same thing:
cast(val,class_res)
valclass(val,class_res)
You can see that there is no point in this, as calling cast directly would be much simpler. Of course if your actual function is longer and more complicated that what you showed us in your question then you can include cast inside of it.
Please click the Accept button if my answer has resolved your question.

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

カテゴリ

Help Center および File ExchangeLogical についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by