1 / 25

Scientific Programming Using MATLAB, Fall 2011-2012

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.

dusty
Download Presentation

Scientific Programming Using MATLAB, Fall 2011-2012

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Scientific Programming Using MATLAB, Fall 2011-2012 TIRGUL #5: Graphics & Debugging

  2. 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

  3. 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')

  4. Styling options • (This is taken from the highly recommended MATLAB tutorial in Hebrew, that can be found on the course website)

  5. 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)

  6. 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

  7. 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

  8. 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’)

  9. 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

  10. 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)

  11. More of these can be found at MATLAB help  text  text properties

  12. 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

  13. 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')

  14. ‘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)

  15. 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

  16. Other 2D plotting functions • pie • scatter • gscatter (group-scatter) • errorbar

  17. 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

  18. Plotting help • help graph2d • Examples: • exampleGraphics2D.m

  19. 3D graphics • Standard plot functions with an extra dimension • Plot3 • Scatter3 • Bar3 • Pie3 • Color-coding • Image • Imagesc • 4D graphics?  3D+color • Example: • exampleGraphics3D.m

  20. 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

  21. 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?

  22. 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

  23. 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

  24. Example: • plotMyParabola.m • plotParabolasScript.m

  25. 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.

More Related