250 likes | 358 Views
Scientific Programming Using MATLAB, Fall 2011-2012. TIRGUL #5: Graphics & Debugging. Simple 2D graphics. x = [0:0.1:10]; y = sin(x); >> plot (x, y) x, y need to have the same length If x is undefined, it defaults as the indices of y. Plotting styles.
E N D
Scientific Programming Using MATLAB, Fall 2011-2012 TIRGUL #5: Graphics & Debugging
Simple 2D graphics • x = [0:0.1:10]; y = sin(x); >> plot(x, y) • x, y need to have the same length • If x is undefined, it defaults as the indices of y
Plotting styles • ‘plot’ by default draws a blue line, but you can draw the actual data points as well • Plot(x,y, ‘color-style’) • plot(x,y, 'mp')
Styling options • (This is taken from the highly recommended MATLAB tutorial in Hebrew, that can be found on the course website)
Figure window • When creating a new plot, MATLAB opens a figure window • Some plotting functions don’t do this automatically, and the figure needs to be defined: figure(#figure) • example : figure(2)
hold on / hold off • Unless told otherwise, any new plot created overrides the last one • Define a new ‘figure’ window for your new plot • If you want both plots in the same figure, use ‘hold on’ between plotting commands • x = [0:0.1:10]; y = sin(x); holdon plot(x, y) plot(x,-y, 'mp') hold off
Figure-handling functions • clf – clear figure • close • Default closes “current figure” – last figure that was changed • You can specify the figure to be closed • close(2) closes figure 2 • close all closes all figures
Other graph properties • axis([xminxmaxyminymax]) • title(‘string’, propertyName, propertyValue) • legend('string1','string2',...) • xlabel • ylabel • grid • text • Example: • title('My Figure Title', … 'fontsize', 16) legend('-sin(x)', 'sin(x)') xlabel('My x-label', … 'fontsize', 14) ylabel('My y-label', … 'fontsize', 14) grid gtext(‘my floating text’)
Text properties • Functions dealing with text can take properties as inputs • (title, xlabel, ylabel, text etc.) • Syntax: • functionName(‘string’, ‘propertyName’, ‘propertyValue’) • Useful properties: • FontSize, FontName, Color, FontWeight • Other properties can be found at help text property list
Text – special cases • Subscript letters/digits – use underscore (_) • An underscore is required before each letter/digit. • Superscript letters/digits – use ^ • Greek letters – use backslash and the name of the letter • Example: • text('\alpha \beta under_s_c_o_r_e pow^e^r', 'fontsize', 16)
More of these can be found at MATLAB help text text properties
Figure editor • Tempting to use, but try not to • you will probably need to output the figure many more times – code will create the desired figure automatically. • File generate m-file
Sub-plotting • subplot(num_rows, num_columns, plot_index) • Figure(1) subplot(2, 2, 1) plot(x, y) title('sinus', 'fontsize', 12) subplot(2, 2, 2) plot(x,-y, 'mp') title('minus sinus', … 'fontsize', 12) subplot(2, 2, [3 4]) plot(x, y) hold on plot(x,-y, 'mp') title('both', 'fontsize', 12) suptitle('Sub-plotting, example')
‘hist’ and ‘bar’ • We want: to display distributions • 2 steps: • Calculate distribution – how many cases in each bin • Plot results • hist(data, nBins) • Or: • distribution = hist(data, nBins) • bar(distribution)
Using ‘bar’ with matrices • bar(Y): if Y is a matrix, bar groups the bars produced by the elements in each row • champMat: • bar(champMat') • legend('Russia', 'Canada', 'Bulgaria', 'Albania', 'USA') • colormap winter
Other 2D plotting functions • pie • scatter • gscatter (group-scatter) • errorbar
Saving figures • File save • Saves as ‘.fig’ file • Can later change figure properties • A heavy, ‘original’, version of the figure • File save as • ‘.png’, for example • A light-weight version of the figure • Good for the final version of your figure • Copy-paste
Plotting help • help graph2d • Examples: • exampleGraphics2D.m
3D graphics • Standard plot functions with an extra dimension • Plot3 • Scatter3 • Bar3 • Pie3 • Color-coding • Image • Imagesc • 4D graphics? 3D+color • Example: • exampleGraphics3D.m
Debugging • Bugs • Syntax errors • no problem, MATLAB errors • Illegal outputs – under some conditions the expression is illegal • More difficult to handle, but still manageable • Wrong output • Your worst nightmare
Handling bugs • Syntax errors • Illegal outputs – under some conditions the expression is illegal • Test your program with all input types and values you can think about • Take care of edge cases • Wrong output • Look at the outputs you get – do they make sense?
lint • Runs in your editor at all times automatically • Shows colored problem-messages : • Critical error • Possible error • Marks the place of the bug • Gives a description of the problem
Debug mode • Put breakpoints ( ) in strategic places • A gray circle ( )– syntax error/ need to save • Press ‘run’ Step in Step out Set/clear breakpoint Exit debug clear all breakpoints step Run/continue
Example: • plotMyParabola.m • plotParabolasScript.m
Practice • In this practice, pay special attention to ‘lint’ messages and use the debug mode to get comfortable about working with breakpoints • Create 2 row vectors of the same size. Plot them one versus the other. • Create another vector of the same size. Plot it versus the first vector on the same graph (use ‘hold on’). • Create a data vector of 50 random grades between 0 and 100. • Create a new figure and plot a histogram of the grades. • Plot a histogram of the grades again, this time divide the data into 5 bins. • Create a new figure and plot a pie chart of the distribution.