how I do it. the function is called a string is passes a numbr
이전 댓글 표시
how could I do this one
looks like
functionname('hello')
ans= hello1
functionname('john')
ans= john2
...
many thanks
댓글 수: 1
dpb
2018년 11월 19일
Which number, in particular? Any one, random, user-specified, sequential since last invocation as shown above (requires some extra logic to keep track of that and over what population if so)...???
So many questions, so little known... :)
s=sprintf('%s%d',str,randi(1000);
might give you some ideas, however...
채택된 답변
추가 답변 (2개)
Luna
2018년 11월 20일
Hello Dezdi,
You can do it by first collecting your inputs in a cell array and then call your function in a for loop with 5 elements.
inputStr = {'John','Mary','Kayle','Adam','David'};
for i = 1:5
out = myFunc(inputStr,i)
end
function outputStr = myFunc(inputStr,i)
outputStr = [inputStr{i}, sprintf('%d',i)];
end
Preethi
2018년 11월 20일
0 개 추천
hi,
check this
function test_preethi(str)
persistent i
if isempty(i)
i=1;
else
if i <=5
sprintf('%s%d',str,i)
i=i+1 ;
else
sprintf('limit reached')
end
end
댓글 수: 3
Stephen23
2018년 11월 20일
The question requests "...and returns the resulting string". Your code does not return anything.
dpb
2018년 11월 20일
This particular function only "works" in that it creates the string internal to the function but you can't get at the result.
The use of persistent is good in saving the previous value, unfortunately the function has no mechanism by which the variable can be reset/re-initialized so without external intervention once the sequence is full, there's no going forward...details, details! :)
카테고리
도움말 센터 및 File Exchange에서 Dates and Time에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!