Classify the values inside edit box (isempty,isinteger, etc.)
1 次查看(过去 30 天)
显示 更早的评论
Hello guys, So i started the code with the following for the variable L :
L = str2double(get(handles.editL,'String'));
W = str2double(get(handles.editW,'String'));
C = str2double(get(handles.editC,'String'));
G = str2double(get(handles.editG,'String'));
H = str2double(get(handles.editH,'String'));
S = str2double(get(handles.editS,'String'));
%SEt the different possibilities of the input and the interaction of matlab
%with the user
%First, The user must not leave empty cases :
if isempty(L)
errordlg('Please enter a number into the text-box. We thank you in anticipation.', 'Error Code I');
set(handles.editL,'String','');
return
%Seconly, the user must enter values of L higher than 0, and return the edit box empty
elseif (L <= 0)
errordlg('Please enter a number that is higher than 0. Thank you.', 'Error Code II');
set(handles.editL,'String','');
return
%Thirdly, the program does not cover greenhouses with L higher than 100 meters, and return the edit box empty
elseif L >= 100
errordlg('Please enter a number that is lower than 100. Thank you.', 'Error Code II');
set(handles.editL,'String','');
return
%finally, the input must be a numerical value
elseif ~isnumeric(L);
errordlg('Please enter a valid number. Thank you.','Error Code III');
set(handles.editL,'String','');
return
end
Finally, i tried to add the above characteristics for S,W,H,G,C (By using or statement for each step and also by using severals if...end statements for each variables) but matlab seems not to accept it in both cases when i run the program. Basicaly, nothing happens (there is no error message, simply that the codes seem not working) Thank you a big time in advance to those who can give me a hint :)
1 个评论
Walter Roberson
2016-11-26
Note: the output of str2double is always numeric; if it was unable to convert something then it returns NaN. Furthermore if it had not been numeric then you would have had a problem comparing the value to a number. When you do have tests for correct data type, you should always place the tests before you use the value.
回答(1 个)
Sid Jhaveri
2016-12-8
1) You must handle the case where there is NaN (as pointed out by Walter Roberson).
2) I am assuming that you like to get only numeric data from your edit box. If this is the case, then you can use "appdesigner" (available in MATLAB R2016a and higher version) which has numeric edit box.
3) You can also refer to the MATLAB Answers links for more helpful information:
0 个评论
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Loops and Conditional Statements 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!