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

30 Ansichten (letzte 30 Tage)
Elena
Elena am 23 Feb. 2022
Kommentiert: Akira Agata am 23 Feb. 2022
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

Akzeptierte Antwort

Akira Agata
Akira Agata am 23 Feb. 2022
How about the following?
function output = yourFunction(input)
if isempty(input)
output = 'unknown';
elseif isa(input,'numeric')
output = input;
else
output = [];
end
end
  1 Kommentar
Akira Agata
Akira Agata am 23 Feb. 2022
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 =
[]

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (0)

Produkte


Version

R2021a

Community Treasure Hunt

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

Start Hunting!

Translated by