Matlab GUI Video Tutorial -1

Its the fact that you can not grasp the things from the articles than you get it from the video tutorials. Here is the video tutorial for Matlab GUI which I searched from the youtube and placed it here. This video tutorial regarding Matlab GUI will teach you about the basics of Graphical User Interface. And by the end of the day you would be able to make the plots like mesh, surf as asked by the GUI setup made by you. After practicing it, you may move on to  Calculator in Matlab GUI. Leave your comments, if you feel difficulty. Thanks.

src="http://www.youtube.com/v/D_hmws6dwgg?fs=1&hl=en_US" type="application/x-shockwave-flash" allowscriptaccess="always" allowfullscreen="true" width="640" height="385">

12:48 | Posted in , , | Read More »

Scientific Calculator in Matlab GUI

Only needs 4 lines of code !!
Today I am going to present you a scientific calculator build in MATLAB GUI. This calculator is capable of solving all the those scientific mathematics that matlab can do. And I was able to get this calculator programmed in 4 or 5 lines of code. In fact it was a matlab command 'eval()' that solved the problem. I really like this command. Couple of days ago, I posted a tutorial on MATLAB GUI and built a simple calculator. If you guys have not read that tutorial yet please go through that first else you will be in trouble because I will be taking shortcuts here. 

So, I hereby assume that you guys know about guide , how to make GUI and view callbacks. They all are already discussed in detail in the tutorial

Ladies and Gentlemen, make a layout of your two line display calculator as shown below: :-



This is not very difficult in fact. Place two push buttons, two static texts and two edit texts ! And you are done. Rest you can make it more and more beautiful by playing with the fonts and calculators.

Call Backs

Look, we will be pressing Answer key and reset key only so we are concerned with callback of theses two buttons in this particular case only. Writing codes for the callback is again very easy. Right click the buttons and view the callbacks in M-file, generated. Lets come to the "Answer" and "Reset" callbacks  individually.

Answer Callback

x=get(handles.no1, 'String');
x
=eval(x);
set(handles.no2, 'String', x);
guidata(hObject,handles);

 Believe me guys, your scientific calculator is ready with the magic of "eval()" command. What the code is doing? First of all its getting data from whatever user has entered. Since in my case the tag for input field is no1 therefore its using handles.no1. It is important to note here that get returns a string. In MATLAB there is a command eval() which solves or evaluate the expressions inside a string. The result thus obtained, is displayed using set command. Thats all the working of the code. 

Reset Code: 

set(handles.no1,'String', 0);
set(handles.no2,'String', 0);

Just to put a little "Shashka" in it, Reset is used here. Its quite evident that on pressing reset all the values will be reset that is "ZERO".
My Results
Here is, how I tested this scientific calculator in different ways. Few of the snaps are given below for  your motivation.
 Another One

More to follow!!

23:01 | Posted in , , | Read More »

Calculator in Matlab GUI

Graphical User Interface - Calculator

Today, I would be talking about GUI interface in Matlab. By the end of the post you would be able to make your own GUI calculator in your own way. Infact, GUI in matlab  is more like a fun. Lest start developing a basic calculator in GUI Matlab. 

1.   Type guide in Matlab Command Window. Following dialog box will open :-
 
.
2.   Selecting "Blank GUI (Default)" will take you to the window of your interest. It would look like :-

3.  From here, go to File , then Preferences, then Guide, and then check all the fields, so that our GUI window looks more comfortable and user-friendly. Here is the snap shot of our GUI work place :-


4. Now using these buttons, try to make a calculator. For example. Place three "edit text" in the panel, two for the inputs and one for the output. You may use number of "static text" in order to improve the visibility. In the end you may use a "push button" to display the result. I have also used four extra push push buttons for addition, subtraction, multiplication and division. Also you may use a panels, e-g as depicted in the figure that I have used three panels namely "operands", "operator" and "operand". Your layout of calculator is almost ready. Above all you must go into the "Inspector" by double clicking the the items individually in order to replace edit text, static text etc into a user friendly format (In Inspector see "String" and "Tag"). You may also change the font and play with colours at your own.  Your calculator may like like :-



5.  After running this GUI ( Click the green button), M-file  for your GUI calculator will be generated. For the time being we are only concerned with the callbacks. As seen above , we have got 4 push buttons, 2 input fields and one output field. We will proceed such that we will save the inputs entered by the user in some variable. Thereafter we will add, subtract, multiply, divide depending on what user wants. Usually we use get function to take input and set to display the output. How to view callbacks? See below :-



6.  Following portion of M-file would be in picture :-

7.   Lets come to the coding of individual callbacks in our GUI calculator. First of all we know that the output field  (where the answer will be displayed) does not need any callback coding. So we are left with only 4 push buttons and 2 input fields. Lets talk about each individually :- (Same method is adapted to view the callbacks in each case)

Ist No Callback
Insert the following code inside the Ist Number callback. Also see the commented lines to grasp the "critical points"

num1= str2double(get(hObject, 'String'));

% get is used to get the data from the input field field
% str2num; because input is always taken as string
% final value is stored in num 1
 
handles.calc.x=num1;

% not NEEDED. will work without  handles.calc.x.num1. % handles.calc.x ; .calc.x may be replaced as desired (abc.z) but through 
% the M-file use the same "calc" like handles.calc.y, handles.calc.z etc

% necessarily be needed if used in some other function
 
guidata(hObject,handles);
 
%  to update the field

2nd No Callback
Similarly for the second input, enter the following inside the callback function

num2= str2double(get(hObject, 'String'));
handles.calc.y=num2;



% not NEEDED. will work without  handles.calc.x.num1.

guidata(hObject,handles);



See also the comments made in Ist No Callback

Operands Callback

Additon:  

add=handles.calc.x+handles.calc.y; 
% adding the two numbers (could not add if we had not used handles.calc.y etc in Ist  % and 2nd no cal l back

set(handles.ans , 'String', handles.calc.add);
% This is used to display the output (i-e additon)
% handles.ans ; ans is the tag for output field (may be viewed and changed in
%  inspector)
 
Subtraction:

sub=handles.calc.x-handles.calc.y;
set(handles.ans , 'String', sub);

Multiplication

mul=handles.calc.x*handles.calc.y;
set(handles.ans , 'String', mul);

Division

div=handles.calc.x*handles.calc.y;
set(handles.ans , 'String', div);


Congratulations! Your Matlab GUI calculator is ready

Now you are just one step ahead to see your calculator working . Click the green button or press F5. Your first working GUI calculator is ready. You can open it again by typing in calc (in my case) or with whatever named you have saved the M-file. The calculator looks like :-

09:46 | Posted in , , | Read More »

Labels

Recently Commented

Recently Added