How to make Matlab Errors not appear (and therefore end a program)
조회 수: 6 (최근 30일)
이전 댓글 표시
I have created a looped employee time clock program that is to take the input of a user, saving their ID# and what new (Abbreviated) task that they are "logging" into, (or only logging out of), to store some information such as date, and time to perform the task.
The code I have provided is ONLY the input(explains the correct format), and bit of re-arranged code so you can see the idea of the program.
The following input allows to program to run w/o error: {1 'ms'} or {2} or {3 'es'} {'End'}
The following input causes a MATLAB error to result, and therefore end the program loop:
1) No space,
2) missing '
3) Missing {}
{1'ms'} {1 'ms} 1 ms
Q1) Now my question is, how can I avoid Matlab Errors from this incorrect format entered in order to avoid ending the program loop that I have? Is there a deeper level of programming in matlab to allow me control these errors, and continue the program loop despite the erroneous input?
Q2) Also, is there a way to automatically add the { } brackets around the user's input, so-that she only has to enter 1 'ms' and not {1 'ms} ?
Thank you very much for your help!! The code is attached as a link, and pasted below:
%TicTocTask_Daily_input_error
a=1;
while a
log_in_out = input(['Enter "{''ID#''}" to only end a task; eg. {2} ...OR...',...
'\nIf starting new task, enter "{ID# ''Task''}", (separted by space); eg. {1 ''ms''}:\n']);
if iscell(log_in_out) == 0 %IF NOT a cell...
disp('Incorrect Format, ensure {} included (i.e. cell format), and '' '' around abbreviated task.....')
disp(' ');
elseif isempty(log_in_out) %If the user enters nothing
disp('Nothing Entered...');disp(' ');
elseif strcmp(log_in_out{1}, {'End'})%ENTER--> {'End'} to END PROGRAM!!
disp('Ended program')
a=0; break
end
end
답변 (1개)
sloppydisk
2018년 5월 3일
I think you will want to simply take the input as text without evaluating, to do this use the 's' option in the input function:
prompt = 'Hello\n';
x = {input(prompt, 's')};
class(x)
class(x{:})
Is this what you are looking for?
참고 항목
카테고리
Help Center 및 File Exchange에서 Characters and Strings에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!