How to accept time from the user into a matrix?

조회 수: 1 (최근 30일)
Febin Benjamin
Febin Benjamin 2014년 6월 28일
댓글: Azzi Abdelmalek 2014년 6월 28일
I want to accept (say)3 time elements for example 8:30, 8:20 & 8:00 from user and store it in an array using 'datenum'. How can i achieve that? Please help.
  댓글 수: 1
Febin Benjamin
Febin Benjamin 2014년 6월 28일
I have worked out my version here if anyone needs it in future.
dateNumArray = []; %// Store datenums here
%// Enter a blank line to quit this loop
while true
timestr = input('Enter a time: ', 's');
if (isempty(timestr))
break;
end
%// Split the string up at the ':'
splitStr = strsplit(timestr, ':');
%// Read in the current date as a vector
%// Format of: [Year, Month, Day, Hour, Minute, Second]
timeRead = clock;
%// Replace hours and minutes with user prompt
%// Zero the seconds
timeRead(4:6) = [str2num(splitStr{1}) str2num(splitStr{2}) 0];
%// Convert to datenum format
dateNumArray = [dateNumArray datenum(timeRead)];
end

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

채택된 답변

Azzi Abdelmalek
Azzi Abdelmalek 2014년 6월 28일
d=input('choose 3 time in this form 8:00 8:30 9:00 ','s')
e=datenum(regexp(d,'\S+','match'))
  댓글 수: 2
Febin Benjamin
Febin Benjamin 2014년 6월 28일
Azzi does your code mean that user can input only 3 specified time elements i.e 8:00 8:30 9:00? No right? User should be able to enter any time (e.g. 4:45 5:56 8:00).
Azzi Abdelmalek
Azzi Abdelmalek 2014년 6월 28일
No , he can enter any number of time he wants, just choose your message

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

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Dates and Time에 대해 자세히 알아보기

제품

Community Treasure Hunt

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

Start Hunting!

Translated by