How to make Matlab Errors not appear (and therefore end a program)
2 visualizaciones (últimos 30 días)
Mostrar comentarios más antiguos
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
2 comentarios
dpb
el 3 de Mayo de 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=input(...
log_in_out=cellstr(log_in_out); % convert to cellstr
or
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..
otherwise
error
end
If there is a predefined set of tasks, you could have those stored and they could only select from them...
Respuestas (1)
sloppydisk
el 3 de Mayo de 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')};
class(x)
class(x{:})
Is this what you are looking for?
Ver también
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!