15
USING GUIDE By Sahil Potnis, VIT, Mumbai Creating a GUI in Matlab TUTORIAL : 2

GUI in Matlab - 2

Embed Size (px)

DESCRIPTION

Second Tutorial for learning the basics of Matlab GUI programming....

Citation preview

Page 1: GUI in Matlab - 2

USING GUIDE

By Sahil Potnis, VIT, Mumbai

Creating a GUI in Matlab

TUTORIAL : 2

Page 2: GUI in Matlab - 2

Expected Outcomes:

By Sahil Potnis, VIT, Mumbai

This presentation will ‘guide’ you to –

Guide stands for GUI Development Environment.

Create a new GUI on your own.

Plot graphs in it and save them.

Page 3: GUI in Matlab - 2

By Sahil Potnis, VIT, Mumbai

Step 1:

Create new GUI by using standard default template available.

Select (UIControls) template, the one described in previous tutorial.

Save it with a title ‘ Linear_equation’, this will generate a linear_equation.m file.

Do not alter the .m code up to line no:74 .

Page 4: GUI in Matlab - 2

By Sahil Potnis, VIT, Mumbai

Create New GUI

Page 5: GUI in Matlab - 2

By Sahil Potnis, VIT, Mumbai

Make two variables ‘X’ and ‘Y’ and correlate them using any linear equation. Find value of ‘Y’ when ‘X’ is given.

Add a graph by selecting axes and another pushbutton for plotting y vs x.

For ex: 5X=4Y + 50. Let user enter the value of X and find value of Y

after pressing calculate.Make sure you enter the formula within callback of

‘calculate’ to find value of ‘y’.Remove unnecessary objects within the code and

check for all tags and handle variables..!

STEP 2:

Page 6: GUI in Matlab - 2

By Sahil Potnis, VIT, Mumbai

An ideal GUI figure should look like this:

Page 7: GUI in Matlab - 2

STEP 3:

By Sahil Potnis, VIT, Mumbai

Within callback of ‘Plot’ button write the code for simple plotting of two variables. Write –

axes(handles.graph); % graph is tag for axes object% Start your code CODE% End your code

This should generate the Plot when you click on Plot.

Hint- Create a function for the plot & call that function in you code for complex equations.!

Page 8: GUI in Matlab - 2

By Sahil Potnis, VIT, Mumbai

Checkpoint 1:

By now you should be ready with –

A GUI that calculates and resets the values of y and x respectively based on your entered equation.

Plots the graph in the space for ‘graph’ when ‘Plot’ is clicked.

Page 9: GUI in Matlab - 2

By Sahil Potnis, VIT, Mumbai

Step 4:

Now, go to the Menu editor option, present in the toolbar on top of your .fig window.

Create a ‘Context Menu’ and add a new context menu ‘save as’.

Now go to Property Inspector of the ‘graph’ and in the option ‘UIContext Menu’ select your option and deselect ‘none’.

Save the file….

Page 10: GUI in Matlab - 2

By Sahil Potnis, VIT, Mumbai

Page 11: GUI in Matlab - 2

By Sahil Potnis, VIT, Mumbai

Page 12: GUI in Matlab - 2

By Sahil Potnis, VIT, Mumbai

Once done, within the callback of ‘saveas’ paste this code –

f = figure;copyobj(handles.graph,f); %graph is a tag for axesFileName = uiputfile('*.jpg','Save as'); saveas(f,FileName);

You can alter the file format as required..Now, when you run your GUI you can save the

graph by right clicking it..!

Page 13: GUI in Matlab - 2

By Sahil Potnis, VIT, Mumbai

Checkpoint 2:

You are ready with completely functional GUI.

It has options for computing a linear equation.

Plotting a graph.

Saving the graph by ‘right clicking’ on graph.

Page 14: GUI in Matlab - 2

By Sahil Potnis, VIT, Mumbai

Keep on trying new things.....Its the best way to

learn.!!

Watch out this space for more tutorials on Matlab....

Page 15: GUI in Matlab - 2

Thank You

By Sahil Potnis, VIT, Mumbai

For further advanced Matlab programming visit : http://sahilpotnis.blogspot.in/

Or drop me a mail at : [email protected]