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

19 次查看(过去 30 天)
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 中查找有关 Characters and Strings 的更多信息

产品


版本

R2021a

Community Treasure Hunt

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

Start Hunting!

Translated by