how to use a function
1 次查看(过去 30 天)
显示 更早的评论
i have a function that convert numbers to arabic letters called " num2wordsarabe" it works good,but when i try to use it directly in "strrep" or any other function ,it does not work ,also i tried to pose x=num2wordsarabe(n) and use "x" in "strrep",it didnt work neither here is the exampl:
for i=1:length(str)
if (abs(str(i))>=48)&&(abs(str(i))<=57)
n=str2double(str(i));
num2wordsarabe(n);
str=strrep(str,str(i),num2wordsarabe(n));
end
end
result on workspace:
[ ??? Error using ==> num2wordsarabe
Too many output arguments.
Error in ==> test at 15
str=strrep(str,str(i),num2wordsarabe(n));]
Any help is greatly appreciated.
4 个评论
John D'Errico
2015-2-21
编辑:John D'Errico
2015-2-21
Please use the code formatting button when you post code. I've fixed it for you.
Andrew Newell
2015-2-21
I reformatted the original question. I assume that the curly brackets were an attempt to format it, not the code.
采纳的回答
Guillaume
2015-2-21
Your understanding of the syntax of functions is a bit wrong. As per Sad's answer, if you want to use the result of a function, that function needs to output something. Therefore your function needs to be at least:
function x = num2wordsarabe(n)
And actually, since x does not mean anything, I'd rather use a variable name with more meaning, e.g:
function arabewords = num2wordsarabe(n)
Within your function, you then of course need to assign some value to the output. Therefore, at some point(s) in the function you should have:
arabewords = ....; %replace the ... by some expression
Most likely, the ... is the output of these hundredarabe, tensarabe and unitsarabe functions whose results you don't do anything with. E.g.:
arabewords = sprintf('%s %s %s', hundredarabe(z), unitsarabe(x), tensarabe(y,x));
%and so on for the other else and if
Possibly, these other function also don't have declared output in them, so you would have to modify them as well the same. e.g:
function hundredword = hundredarabe(digit)
...
hundredword = ...;
end
1 个评论
Sad Grad Student
2015-2-21
编辑:Sad Grad Student
2015-2-21
yes! that's exactly my concern as well! Guillaume explained it better! ;)
更多回答(1 个)
Sad Grad Student
2015-2-21
Try making the first line of your function as:
function [x] = num2wordsarabe(n)
And now use it in strrep. I'm no expert in Matlab but give that a try!
2 个评论
Andrew Newell
2015-2-21
Just
function x = num2wordsarabe(n)
will also work. And the line
num2wordsarabe(n);
isn't needed.
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Logical 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!