forked from jooh/matlab-studytools
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathinputvalid.m
More file actions
58 lines (54 loc) · 1.69 KB
/
Copy pathinputvalid.m
File metadata and controls
58 lines (54 loc) · 1.69 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
% Get input that is restricted to certain classes or specific values
% o = inputvalid(question,[validoptions],[validclass])
function o = inputvalid(question,validoptions,validclass)
% TODO repair findStrInArray
if ieNotDefined('validclass')
validclass = '';
end
if ieNotDefined('validoptions')
validoptions = '';
end
optcell = ~isempty(validoptions) && iscell(validoptions);
optstr = ~isempty(validoptions) && isstr(validoptions);
optnum = ~isempty(validoptions) && isnumeric(validoptions);
done = 0;
while ~done
o = input(question,'s');
onum = str2num(o);
if ~isempty(onum)
o = onum;
% Not good enough if we want a str
if strcmp(validclass,'str')
fprintf('invalid response. Try again.\n')
continue
end
% Check against numbers
if optnum && ~any(o == validoptions)
fprintf('invalid response. Try again.\n')
continue
end
% Check against cell array of numbers
if optcell && ~any(findStrInArray(validoptions,o,1))
fprintf('invalid response. Try again.\n')
continue
end
done = 1;
else
% not good enough if we want a num
if strcmp(validclass,'num') || optnum
fprintf('invalid response. Try again.\n')
continue
end
% Check against str
if optstr && ~any(strfind(validoptions,o))
fprintf('invalid response. Try again.\n')
continue
end
% Check against cell array of strings
if optcell && ~any(findStrInArray(validoptions,o,1))
fprintf('invalid response. Try again.\n')
continue
end
done = 1;
end
end