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

Sad Grad Student
Sad Grad Student 2015년 2월 21일
Can you paste the code of your function here?
ben zekri raouf
ben zekri raouf 2015년 2월 21일
편집: John D'Errico 2015년 2월 21일
function num2wordsarabe(n)
%NUM2WORDS FUNCTION
% num2words is a matlab function that converts number to words. Num2words
% cannot convert four digit numbers and above.
% To use num2words just enter your 3, 2 or 1-digit number. ie
%
% num2words(123)
%
%REQUIREMENTS
% num2words require four other m-files to function.
% aaand.m
% hundred.m
% tens.m
% units.m
% These function m-files must all be placed at the same matlab search path.
% In num2words, information about each of the digits of the number, n,
% entered is passed to the functions: hundred.m, tens.m and units.m for.
% The resulting words is then printed neatly at command window
%
% Author: Mr. Emeka Nwaobasi
% Date : 6th April, 2014
% Copyright 2014
% error(nargchk(1,1,nargin,'struct'))
if isnan(n)
error('Input argument must be a number')
elseif n < 0
error('Input argument must be a positive integer')
elseif ceil(n) ~= n
error('Input argument must be an integer')
elseif ~isscalar(n)
error('Input argument must be scalar')
elseif n == 0
fprintf('\nZero\n\n')
return
end
flag = 1000; flag1 = 10; flag2 = 100;
% check number of digit to know if limit has been violated
chkdgt = n/flag;
chkdgt = floor(chkdgt);
if chkdgt > 0
error('Maximum number of digits, three(3), exceeded.')
end
%--- check for number of digits ---
if floor(n/flag2) > 0
%fprintf('\nThree digit number\n')
%fprintf('------------------\n')
%--- knowing what each digits of the now known 3 digit number is ---
%--- for 1st digit ---
z = floor(n/flag2);
%--- for 2nd digit ---
sdigit = n - 100*z;
y = floor(sdigit/10);
%--- for last digit ---
ldigit = n - 100*z - 10*y;
x = ldigit;
if x == 0 && y == 0
hundredarabe(z)%,fprintf('\n\n')
elseif y == 1
%--- to prevent reading, for example: 118 as one hundred
%--- and eighteen eight ---
hundredarabe(z),tensarabe(y,x)%,fprintf('\n\n')
else
hundredarabe(z),unitsarabe(x),tensarabe(y,x)%,fprintf('\n\n')
end
%--- check for number of digits ---
elseif (floor(n/flag2) == 0) && ((9 - n) < 0)
%fprintf('\nTwo digit number\n')
%fprintf('----------------\n')
%--- knowing what each digits of the now known 2 digit number is ---
%--- for 1st digit ---
y = floor(n/flag1);
%--- for 2nd digit ---
x = n - 10*y;
if y == 1
%--- to prevent reading, for example 18 as eighteen eight ---
tensarabe(y,x)%,fprintf('\n')
else
unitsarabe(x),tensarabe(y,x)%, fprintf('\n')
end
%--- checking for single digit ---
elseif (9 - n) >= 0
%fprintf('\nSingle digit number\n')
%fprintf('-------------------\n')
unitsarabe(n);%,fprintf('\n\n')
end
end
John D'Errico
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
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
Guillaume 2015년 2월 21일

1 개 추천

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
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
Sad Grad Student 2015년 2월 21일

0 개 추천

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

Just
function x = num2wordsarabe(n)
will also work. And the line
num2wordsarabe(n);
isn't needed.
ben zekri raouf
ben zekri raouf 2015년 2월 21일
thank you so much for your efforts,but unfortunately it didn't work,thnx any way :)

댓글을 달려면 로그인하십시오.

카테고리

도움말 센터File Exchange에서 Characters and Strings에 대해 자세히 알아보기

질문:

2015년 2월 21일

편집:

2015년 2월 21일

Community Treasure Hunt

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

Start Hunting!

Translated by