Info

This question is closed. Reopen it to edit or answer.

How can I retrieve time stored in a variable and print it next to previous time in a text file in Matlab.

1 view (last 30 days)
I am working on a time stamp code using GUI. I have four radio buttons in my interface and I want to stamp the date and start and end time when I press each radio button. This means that every time I select a radiobutton, I want that time related to it to be stored and printed as the end time of the last selected radiobutton and the start time of the currently selected radiobutton. My problem is that the endtime winds up being less than the start time and I don't know how to correct that. I would really appreciate some help. Thanks!
function uibuttongroup1_SelectionChangedFcn(hObject, eventdata, handles)
filename = 'test.txt';
fileID = fopen(filename,'at');
A = get(hObject,'String');
date = datetime('now','Format','MM/dd/yyyy');
dat = char(date);
time = datetime('now','Format','HH:mm:ss');
currentTime = char(time);
endtime = getappdata(0,'futuretime');
endtime = char(endtime);
%elapsedtime = char(time - prevTime);
fprintf(fileID,'%s\n',[]);
fprintf(fileID,'%s\t %s\t %s\t %s\n',A,dat,currentTime, endtime);
fclose(fileID);
setappdata(0,'futuretime',time);

Answers (1)

Walter Roberson
Walter Roberson on 29 Dec 2015
You are getting your "endtime" from the existing getappdata(0,'futuretime') which was set by a previous call's current time. "endtime" is going to be in the past. You should be expecting it to be before your current time. You seem to have your past and future reversed.
  1 Comment
Nicolas Rodriguez
Nicolas Rodriguez on 29 Dec 2015
I tried reversing them but it didn't work. As soon as I keep appending more data from a text box function that is down below the code it records a random time that is not the start of the selected radio button. I don't know if it has something to do with the push button function that is part of the code. Here is that function:
function pushbutton2_Callback(hObject, eventdata, handles)
global filename;
fileID = fopen(filename,'at'); %keeps appending whatever you input in...
%edit box.
SRB = get(get(handles.uibuttongroup1,'SelectedObject'),'String');
date = datetime('now','Format','MM/dd/yyyy');
dat = char(date);
time = datetime('now','Format','HH:mm:ss');
currentTime = char(time);
texto = get(handles.edit1,'String');
fprintf(fileID,'%s\t %s\t %s\t %s\n', SRB,dat,currentTime,texto);
set(handles.edit1,'String','');
fclose(fileID);
uicontrol(handles.edit1); %clears the textbox

Community Treasure Hunt

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

Start Hunting!