سؤال
أحاول ما يلي الآن:
function isNum = isItANum(string)
isNum = isempty(str2num(string))
end
المشكلة هي إذا كان لدي تاريخ "1998/34/2"، أريد وظيفتي أن أقول لا.
المحلول
من عند help str2num
:
*Caution:* As `str2num' uses the `eval' function to do the
conversion, `str2num' will execute any code contained in the
string S. Use `str2double' instead if you want to avoid the use
of `eval'.
See also: str2double, eval
يبدو أنك يمكن أن تحل محل وظيفتك مع ~isnan(str2double(string))
نصائح أخرى
قم بعمل حلقة بحيث تقوم بتقسيم السلسلة بأحرف واحدة، وإذا فشل أي شار، ثم إرجاع 0.
لا تنتمي إلى StackOverflow