return value of [ ] for an 'if' or 'for' function

조회 수: 28 (최근 30일)
Elena
Elena 2022년 2월 23일
댓글: Akira Agata 2022년 2월 23일
Say I have any function and the input can be input = 123.
For this function, only numbers can work as the input or else the return value should be [ ].
So if i were to have input = 'abc', the return value would need to show [ ].
How can I do this? If the input passes that first test it needs to be able to run the rest of the code.
Here is something i tried, it did not work.
Distance = 'abc'
if Distance = lettersPattern
res = []
end
Additionally, how can i also link the part abvoe to another requisite. If the input is empty to say 'unknown'?
if isempty(cLine)
res = 'unknown'
end

채택된 답변

Akira Agata
Akira Agata 2022년 2월 23일
How about the following?
function output = yourFunction(input)
if isempty(input)
output = 'unknown';
elseif isa(input,'numeric')
output = input;
else
output = [];
end
end
  댓글 수: 1
Akira Agata
Akira Agata 2022년 2월 23일
Well, in that case the Regular expression will work, like:
function output = yourFunction2(input)
str = regexp(input,'^\d{3}-\d{2}-\d{2}$','match');
if ~isempty(str)
output = input;
else
output = [];
end
end
For example:
>> yourFunction2('000-01-00')
ans =
'000-01-00'
>> yourFunction2('000-01-0a')
ans =
[]

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

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Logical에 대해 자세히 알아보기

제품


릴리스

R2021a

Community Treasure Hunt

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

Start Hunting!

Translated by