How to write m file code in GUI

2 views (last 30 days)
puja dutta
puja dutta on 15 Jun 2014
Commented: Geoff Hayes on 28 Jun 2014
My code is for finding dwell(time to hold the key) and flight time(time interval between key released and next key press) when the user type a password/username.
function Keypress_set
x1=0;
x2=0;
x3=0;
f=0;
h = figure;
set(h,'KeyPressFcn',@KeyDownCb,'KeyReleaseFcn',@KeyUpCb)
tic;
function KeyDownCb(~,evnt)
out = sprintf('Key: %s\n',evnt.Key);
disp(out)
x1=toc;
f=x1-x2;
disp(x1)
disp(f)
end
function KeyUpCb(~,evnt)
out = sprintf('Key: %s\n',evnt.Key);
disp(out)
x2=toc;
disp(x2)
x3=(x2-x1);
disp(x3)
end
end
i want to write it in GUI and display f and x3 in different text box(i display key ,x1,x2 just to check the answer,not necessary). Please help me.I dont know the format propely for writing GUI.

Accepted Answer

Geoff Hayes
Geoff Hayes on 17 Jun 2014
Similar to your code above, you will need to add two callbacks to the GUI. In the GUI editor, from the menu, select View-->View Callbacks and select KeyPressFcn and the function signature and empty body will be added to the m file for this GUI. Repeat this callback selection and choose KeyReleaseFcn. In your m file, you will now have something similar to
% --- Executes on key press with focus on figure1 and none of its controls.
function figure1_KeyPressFcn(hObject, eventdata, handles)
% --- Executes on key release with focus on figure1 and none of its controls.
function figure1_KeyReleaseFcn(hObject, eventdata, handles)
Now the code for each of these callbacks will be very similar to what you did already except that the f, x1 and x2 (and maybe x3) cannot be the local variables that you have defined above but will be members within the handles struct that you will update on each press and release. For example, the figure1_KeyPressFcn body may look something like
function figure1_KeyPressFcn(hObject, eventdata, handles)
out = sprintf('Key: %s\n', eventdata.Key);
disp(out);
% get and set the variables in the handles structure
handles.x1=toc;
handles.f=handles.x1-handles.x2;
% now save the data
guidata(hObject,handles);
% now display the data in the edit boxes
set(handles.editf,'String',num2str(handles.f));
set(handles.editx1,'String',num2str(handles.x1));
The above assumes that you have created two edit text boxes named editf and editx1. The figure1_KeyReleaseFcn would be written in a similar fashion. Note that you will have to initialize the f, x1 and x2 (and maybe x3) like you have but in the handles structure. This logic can be added to your figName_OpeningFcn function as
handles.f = 0;
handles.x1 = 0;
handles.x2 = 0;
handles.x3 = 0;
tic;
just prior to the guidata(hObject,handles) call within that function. The above initialization is just like yours, including the tic command too. Note that we call the guidata function to save your data to the handles structure.
Try the above and see what happens!
  22 Comments
puja dutta
puja dutta on 28 Jun 2014
Geoff- The code you gave DwellFlightCalculator.m starts the working after a key is pressed.How can i make the working after each password attempt
I mean like the flight time must come in the way(just an example)
0 0.0700 0.0695 0 0.0747 0.0767 0 0.695 0.0712
Please help me
Geoff Hayes
Geoff Hayes on 28 Jun 2014
Puja - the answer to that is in my previous two comments. I urge you to put a breakpoint in the code and step through it and see why it is not working and try to understand what the code is doing and what the error message (if any) means and how to address it.

Sign in to comment.

More Answers (1)

puja dutta
puja dutta on 17 Jun 2014
This is so embarrassing . I got an email that john has answered to my question but here i am not being able to see any answer.Its really urgent,i need the answer please help me.
  2 Comments
ES
ES on 17 Jun 2014
What all do you want to display? Which of them are dynamic? when should the function be called (eg., on clicking a button in the GUI etc)?
puja dutta
puja dutta on 18 Jun 2014
i want to display f and x3.the function should be called when i enter some key in a text field which i will name as password.

Sign in to comment.

Categories

Find more on Programming in Help Center and File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!