September+2012

MATLAB Beginnings
To begin MATLAB this week, I started reviewing the first 3 chapters from last year. I then went to the [|mathworks.com] and did a problem on Cody. I tried to solve a problem determining whether a number is even or odd. And another coding problem that was much simpler, just completing the operation of addition and then storing it as a variable name and displaying it. toc

to improve on the code I wrote above, I need to display the function of a+b=c and also reverse the order of a+b, the c should have come first. and I added the ; so that it would not show the a= and b= value when running the code on a program. all that would be displayed is c at then end. And then the 'end' stops the program from continuing.

Running .m Tool
Above is the code for .m Tool which went through and explained the main programing for GUI. .m tool talked about each step in creating the .m code for a GUI. It began with setting the basic parameters for the code and then got into designating each section. It also talked about setting handles and working on the callback function. I remember editing callback parameters last year while trying to program my own GUI. GUI coding is all done separately and as you go in creating one in MATLAB. The tool outlines all of the execution lines and I read about debugging each execution line as you go in your program, constantly checking for mistakes done throughout your coding.

the coding above shows hints and explains each different part with the %. When coding, the % are used as notes for the creator and are not read by the program. They are very useful for going back and looking at each step of the code as you move further and further on.

In Review
I began reviewing all of the old matlab programs and files I finished last year. Beginning with simple animations going all the way up to my unfinished MATLAB GUI code. I corrected and played with my simple starbounce animation. The first line tells you how many blank spaces to move, the second tells how long to pause between each bounce then the script is written below. Almost everything is already defined above so all the continues to filling in the blanks to make the star bounce. Then after saving, you can run the program and in the bottom left hand corner screen, the star bounce begins making a zigzag pattern back and forth across the screen.



Another program that I looked at from last year was my counting program. It simply displays number (k) 1 through 7 which can be changed to any number but we just started off small going to 7. First you begin by defining what N is or the number to which you will count to. Then you use a for statement instructing that k=1:N in which you count by 1s and go until N. Then add display K and by putting num2str turns the numbers into a string to be displayed. Then ending the program and adding disp('Done!') for the user to see.



Current Project : GUI
GUI stands for graphical user interface and is an interactive tool in which I will create on MATLAB. First I began by using a code written by another student for slope and my hopes are to turn it into a GUI of my own. The beginning function is simple, ask the user for the y-intercept, xmax, xmin, and the slope of the line. Then you plug the inputs into y=mx+b; and then press display and the graph will nicely appear for you. The next part is turning the display tool into a GUI which is more interactive. Instead of using the direct program, the GUI is more direct and can have more features and tends to be more user friendly.

[|Helpful Hints] :
the link above goes very simply through how to make a basic matlab gui similar to what I am working on. It defined a lot of vocab that I didn't know before or barely understood and also touched on a few things I am working on. The link is from the ucla math website.

code:

%graphing a line b=input('y-intercept: ');

m=input('slope: ');

h=input('xmax: ');

l=input('xmin: '); x=linspace(l,h); y=m*x+b;

plot(x,y);

New Code:
%graphing a line

function Slopef(handles) b=get(handles.xEdit2,'Value');

m=get(handles.xslope2,'Value');

h=get(handles.xmax2,'Value');

l=get(handles.xmin2,'Value');

N=300;

x=linspace(l,h,N); y=m*x+b; plot(handles.xAxes1,x,y);

end



And this is my finished GUI. It is very simple and tells the user no instructions and the y and x axis are not labeled but it works. I still have to make the overall GUI more functional but all the handles are correct and the GUI can access each area it is asked. If you look at my original code, instead of using handles, I used input that would ask the user to just type in what he or she is asked. But now in the GUI, the user can simply type the number into the edit box and press enter and the results will be displayed in the axes area, Also I had to add a maximum which is the N value. If you look at the linspace displayed as x= the N is added after the x min and max.



And then I added the label to the x and y axis just very simply as x-axis and y-axis. I also added the equation of a line above the panel and changed the color of a few things. Very simple but can be very appealing to the eye.

Quadratic Formula GUI
After completing the slope GUI, I am now going to do the same thing for a quadratic formula equation. The equation is more complicated and involves the roots of the equation so I will have to connect more parts than before.

Quadratic Original Code:
%quadratic formula disp('ax^2+bx+c') a=input('a=')

b=input('b=')

c=input('c=') x1=(-b+sqrt(b^2-4*a*c))/(2*a)

x2=(-b-sqrt(b^2-4*a*c))/(2*a) h=input('xmax: ');

l=input('xmin: '); x=linspace(l,h); y=a*x.^2+b*x+c; disp('roots')

disp(x1)

disp(x2) plot(x,y)



New Quadratic Code:
%quadratic formula

function quadratic(handles)

a=get(handles.xEdit1,'Value');

b=get(handles.xEdit2,'Value');

c=get(handles.xEdit3,'Value'); h=get(handles.xmax1,'Value');

l=get(handles.xmin1,'Value');

N=500;

x=linspace(l,h,N); y=a*x.^2+b*x+c; plot(handles.xAxes1,x,y)

end

And this is my quadratic GUI after its been completed. You can change the parameters and put in your a,b, and c values.

MATLAB Summary
Overall, GUI stands for graphical user interface and makes programming easier for the user, not exactly true for the programmer. First you have to start with a plan; know the function you are going to be using well and know the direction in which you are taken it even more. Start by writing a simple code for the function like how I did a slope program. Know the names of your variables and how to change them and what each command will do for you. Then after writing your code, test it. Make sure it is clear and runs well before trying to build a GUI off of it; because if the code does not run perfectly, it will be harder to spot the problem in future debugging. After writing the code, it is easiest to then open the default GUI template and begin building from there. I started with placing all my static and edit text boxes and then placing my axes. After everything is where you want it, open the property inspector and begin renaming all the strings and tags to something that is easy to remember. The string is what will show up on your GUI and the tag is what you will use for the function behind the GUI. After everything is set (remembering what you named each variable), save and run the GUI. Absolutely nothing will happen but it will appear to look complete. But it will not work because nothing is written in the callback and your original function is in no way connected to the GUI. Next begin connecting the dots by rewriting your original function using handles on each variable in your function and its corresponding box on the GUI. Then you can connect between the GUI script and the actual interactive GUI. I learned that you only edit the callback functions and leave the rest untouched, it will do what it needs without any script being put in. Then after much coding and connecting, the function you originally wrote should be transformed and be compatible with the GUI created. Then when you run your GUI it will actually work. It can dip into the function you wrote behind it and do exactly what you ask of it.

I have learned much of the basis for creating a simple and a little bit more complicated GUI. I learned a lot of MATLAB's language and what each function does and how important spelling and checking your function as you go is. I have also learned how to set up a GUI properly and learned more about some functions I already knew.