loop for clock in MATLAB

hi all, I wanna do loop for an output to be like this
12:00
12:15
12:30
12:45
13:00
13:15
13:30
13:45
14:00
how can I get this output in MATLAB??? please help and advice...

댓글 수: 2

Jan
Jan 2012년 7월 5일
편집: Jan 2012년 7월 5일
Please add any details by editing the question, because it is not clear, what you want to achieve and which level of variability you want.
In addition it is recommended to use meaningful tags, because they are used to classify the questions. Nearly all questions concern "Matlab code". I'm not able to give better tags by my own, because you did not specify, if your problem concerns a TIMER, SPRINTF, FPRINTF to the command window or to a file, a GUI, etc.
Samer Husam
Samer Husam 2012년 7월 5일
I have GUI the user set the step up value (interval) of 15, 30 or 60 mins, in order to write these results in excel file each value in a cell of column A.
it suppose to go from 12:00-14:00 but with different interval based on user choose.

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

 채택된 답변

Andrei Bobrov
Andrei Bobrov 2012년 7월 5일
편집: Andrei Bobrov 2012년 7월 6일

0 개 추천

try
datestr(datenum(0,0,0,12,(0:15:15*9)',0),'HH:MM')
ADD after Samer's answer
m=15;
id = [6 16];
time1 = cellstr(datestr(datenum(0,0,0,12,m*(1:diff(id)+1),0),'HH:MM'));
xlswrite('1.xlsx',time1,1,sprintf('A%d',id(1)));

댓글 수: 5

Samer Husam
Samer Husam 2012년 7월 5일
is it possible to do it as loop of interval of 15 mins ?
Samer Husam
Samer Husam 2012년 7월 5일
because the interval might change based on user selection..
Jan
Jan 2012년 7월 5일
@Samer Husam: You can edit your comments and do not have to post two different ones.
What exactly does "do it as loop of interval 15 mins" mean? What is the difference between the reuslts of the shown code and your expectations?
Samer Husam
Samer Husam 2012년 7월 5일
the user can set to change the time by 15, 30 and 60 mins. after that I need to write it in excel file, so i need to do it in a loop in order to achieve that..
Kevin Claytor
Kevin Claytor 2012년 7월 5일
Looking at the documentation for datenum (<http://www.mathworks.com/help/techdoc/ref/datenum.html>) it looks like the minute increment is set by the line;
(0:15:15*9)'
in his code above, you would just have to change that to get different increment values (replace 15 -> 30 or 60).

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

추가 답변 (4개)

Luffy
Luffy 2012년 7월 5일
편집: Luffy 2012년 7월 5일

0 개 추천

Clock=char('12:00','12:15','12:30','12:45','13:00','13:15','13:30','13:45','14:00');
for i = 1:size(Clock,1)
Clock(i,:)
end
Does this help you

댓글 수: 4

Samer Husam
Samer Husam 2012년 7월 5일
is it possible to do it as interval of 15 mins ?
what does interval of 15 mins mean,i think it is already as interval of 15mins(12:00,12:15)
Luffy
Luffy 2012년 7월 5일
편집: Luffy 2012년 7월 5일
whatever interval u want say i,
try this:
datestr(datenum(0,0,0,12,(0:i:i*9)',0),'HH:MM');
if u want more values change 9 to number of values u need.
Samer Husam
Samer Husam 2012년 7월 5일
the reason I need it to be in a loop cause I am going to write these results in excel file so its would be useful if its possible to do it in a loop..

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

Kye Taylor
Kye Taylor 2012년 7월 5일

0 개 추천

Try running this script... It has some ideas you might use, but it is hardly optimal.
startTimeString = '12:00';
stopTimeString = '14:00';
startTimeDV = datevec(startTimeString);
stopTimeDV = datevec(stopTimeString);
timeInterval = input('Enter the time step in minutes.\n');
thisTime = startTimeDV;
while thisTime(4) < stopTimeDV(4)
% a hack to count +60 minutes as hour
thisTime = datevec(datestr(thisTime));
thisTimeString = datestr(thisTime);
disp(thisTimeString(end-7:end))
thisTime(5) = thisTime(5) + timeInterval;
end
Luffy
Luffy 2012년 7월 6일

0 개 추천

If you just want a loop with only 3 different intervals maybe this crude method can help Clock15=char('12:00','12:15','12:30','12:45','13:00','13:15','13:30','13:45','14:00');
Clock30=char('12:00','12:30','13:00','13:30','14:00');
Clock60=char('12:00','13:00','14:00');
based on gui if u use popup menu use switch case and give either of Clock15/Clock30/Clock60 to Clock and write a loop,
for i = 1:size(Clock,1)
Clock(i,:)
end
Does this help you

댓글 수: 1

the reason I need the loop cause I need to write it in excel later on, so i tired to make it something like that :
mins=15;
for i=6:16 %number of cell I want to write in.
time= datestr(datenum(0,0,0,12,mins,0),'HH:MM');
range = sprintf('A%i', i);
xlswrite('C:\Users\Samer\Desktop\Excel 1.xlsx',time,'Sheet1',range)
mins=mins+15;
end
but when its write it on excel it doesn't write the hole time in one cell, its each digit in one cell, do u have idea how to over come this ??? and thanks for ur previous idea..

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

Samer Husam
Samer Husam 2012년 7월 6일

0 개 추천

hi all, first I would like to thank you all for your cooperation and I really appreciate it, its all get the work, but the one I need it in my work is as follow:
mins=15;
for i=6:16 %number of cell I want to write in.
time= datestr(datenum(0,0,0,12,mins,0),'HH:MM');
range = sprintf('A%i', i);
xlswrite('C:\Users\Samer\Desktop\Excel 1.xlsx',time,'Sheet1',range)
mins=mins+15;
end
but the problem that I face now is its right each digit in one cell, so how could I overcome this problem ??
your help really appreciated. thanks

댓글 수: 6

Jan
Jan 2012년 7월 6일
As far as I remember, the range must be declared as "A1:A1" for a single Excel cell.
What does "is its right each digit in one cell" mean? Could you please describe the problem more detailed?
this part:
range = sprintf('A%i', i);
I use it to make loop for column A it means start writing from A6 to A16, but what it do is like this: (its suppose to write 12:00 on cell A6)
A6=1
B6=2
C6=:
D6=0
E6=0
and so on for the rest for the loop, so how can i come over this ? thanks for your help in advance.
Jan
Jan 2012년 7월 7일
@Samer Husam: I guess, that this output is what you see in Excel, but it would be better if you explain this. I cannot guess, which code you have used to create this. Anyhow, Please read my comment and even better: "doc xlsread". There you find:
To specify a range (even a range of a single cell), include a colon character in the input string (e.g., 'D2:H4')
Therefore the range of a single cell must be "A1:A1".
Andrei Bobrov
Andrei Bobrov 2012년 7월 7일
My variant is bad?
Samer Husam
Samer Husam 2012년 7월 7일
@Jan Simon: I tried to specify it also but it didn't write in one cell, it write as previous exactly..
Samer Husam
Samer Husam 2012년 7월 7일
@Andrei: yours is working, thanks a lot for you and for all of you who suggest really useful ideas.

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

카테고리

도움말 센터File Exchange에서 Loops and Conditional Statements에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by