How to make Matlab Errors not appear (and therefore end a program)

조회 수: 6 (최근 30일)
Matthew Cribb
Matthew Cribb . 2018년 5월 3일
댓글: Matthew Cribb . 2018년 5월 4일
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:
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
  댓글 수: 2
Matthew Cribb
Matthew Cribb 2018년 5월 4일
Thank you very much for your quick response! I have been going crazy over how many ways my program can mess up, and keep finding more! UGH! I very much agree, that is extremely tough letting the user input info. Even if it doesn't produce a matlab error, I have to track what task has been finished, and what task is in progress for example in order to prevent users from re-doing a task a second time.
Thank you very much for your suggestion on the selection menu options...and switch statements. I am absolutely going to use it and redo my program! I'v been trying to think of how I could effectively use lists/dialog boxes, and that is it...huge thanks.

댓글을 달려면 로그인하십시오.

답변 (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')};
Is this what you are looking for?
  댓글 수: 1
Matthew Cribb
Matthew Cribb 2018년 5월 4일
Not quite, but thanks for the feedback! I wanted the user to be able to quickly enter both their ID and the task they wanted to complete in one sweep. Now Im going to try menu options instead as dpb suggested, which seems like a great idea, and wayyy easier than what I'v been trying to do. My program has become nuts, even though it works relatively well.

댓글을 달려면 로그인하십시오.


Help CenterFile Exchange에서 Characters and Strings에 대해 자세히 알아보기

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by