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

2 Ansichten (letzte 30 Tage)
Matthew Cribb
Matthew Cribb am 3 Mai 2018
Kommentiert: Matthew Cribb am 4 Mai 2018
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 Kommentare
dpb am 3 Mai 2018
Yeah, all of the above... :)
With input you get a string returned but can't control what the user types (as you've learned) so error handling is tough...the first part about the cell string is easy though, when you get the char string back, convert it to cellstr inside your code; the user doesn't need to know how it is stored internally. You might instead of the cellstr want to use the newer strings class...
log_in_out=cellstr(log_in_out); % convert to cellstr
log_in_out=string(log_in_out); % convert to string
As for making the interface easier plus limiting the amount of error handling, use menu to force the user to select a specific action from the limited choices provided -- that can be something like
option=menu('Select task','Log Out','New Task'); % present choices
switch option % handle selected
case 1
handle logout here...get the task # whatever else..
case 2
handle new task here...again get a task, whatever..
If there is a predefined set of tasks, you could have those stored and they could only select from them...
Matthew Cribb
Matthew Cribb am 4 Mai 2018
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.

Melden Sie sich an, um zu kommentieren.

Antworten (1)

sloppydisk am 3 Mai 2018
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 Kommentar
Matthew Cribb
Matthew Cribb am 4 Mai 2018
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.

Melden Sie sich an, um zu kommentieren.


Mehr zu Characters and Strings finden Sie in Help Center und File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by