MATLAB: How to check input from GUI is char type

guiuicontrol

hi,
I creating transfer function using sys = tf(Numerator,Denominator) which only accept user to key in numbers for Numerator and Denominator. Is it possible to prompt an error message when user try to key Numerator = [1] & Denominator =[s^2 + 3s + 1] ?

Best Answer

num_str = input('Enter Numerator as vector of numbers', 's');
num_str = regexprep(num_str, {'^\s*\[\s*(.*?)\s*\]\s*$', '\s*,\s*', '^\s*', '\s*$'}, {'$1', ' ', '', ''}, 'lineanchors'); %remove possible [] . change commas to spaces, trim leading trailing spaces
mask = ismember(num_str, '0123456789.+-eEiIjJ ');
if ~all(mask)
error('Unrecognized numeric character(s) in numerator: "%s"', num_str(~mask));
end
num_parts = regexp(num_str, '\s+', 'split');
num_values = str2double(num_parts);
bp = find(isnan(num_values), 1);
if ~isempty(bp)
error('Unrecognized number form in numerator: "%s"', num_parts{bp});
end
After this the numerator is in num_values
This code permits users to use [] around the vector of numbers, and permits them to use commas or spaces (but no semi-colons). It permits floating point numbers, including complex values. Complex parts must, however, be entered with no space after the real part, such as 3+5i and not 3 + 5i
This code does not permit use of 'd' or 'D' for floating point, and does not permit the use of hexadecimal or of octal escapes.
The code could be made shorter if you wanted to get rid of the way it tries to show which particular part of the input is disallowed.