Está en la página 1de 4

Using the MATLAB Publisher

January 18, 2013

Starting a new MATLAB script


In the MATLAB menu, select File New Script or click on the icon that looks like a piece of paper on the far left of the toolbar. This should open a new window called the editor where you can write a piece of code called a script. Save the script with a descriptive name such as PublisherTutorial.m All MATLAB script lenames need to end with the extension .m and can contain letters, numbers, and underscores but no spaces or other punctuation, or else the le will fail to run.

Identifying Information
Begin all labs with the following information: %% Your name % Course Title % Lab Number: Lab Title % Due Date For example: %% % % % Samuel S. Student ECE 203 Lab 27: To Infinity and Beyond 1/25/13

The double percent sign (%%) indicates to the publisher that you are starting a new section. The single percent sign (%) indicates comments within a section. Both are considered comments in the MATLAB code and will appear as green text.

Important Note About Spacing


There must be a space between the (%%) and the title of each section for MATLAB to recognize it as a new section. This will start a new section: %% This is a new section. However, the same code without the space will not make a new section: %%This is not a new section. Furthermore, when making additional comments, a single space between the (%) and the text will cause MATLAB to reformat your text with word wrap, while two spaces will tell MATLAB to leave the spacing exactly as you type it. % With one space, my text will be wrapped when I publish, % so none of my words are cut off.

% % % %

With two spaces, I can put text on different lines however I want but MATLAB wont wrap the text if it goes past the end of the page

This means that when you enter the identifying information at the top of your lab, you can control which lines the text is on by using two spaces.

Publishing
Enter your identifying information at the top of your new script exactly as shown above, and nd the Publish icon on your editor toolbar. It looks like a slanted sheet of paper with horizontal lines coming o of it. Click on the icon to open a new window with your published code. Rather than using the publisher, most people use the Run icon when executing code. This is the icon on your toolbar that looks like a square with a green triangle.

Lab Body
Each lab is divided into sections. These should be labeled in your published lab writeup as separate sections. This means that every section should start with a double percent sign (%%). Whatever words you write after the (%%) will be automatically gathered into a table of contents at the top of the document. Try entering the following code and comments underneath your lab information, and publish it. Dont worry if you dont understand what the code is doing, as this will be covered in Lab 1. This exercise is meant only to make you familiar with formatting your lab writeup. %% Part 1: Some Code %% (a) a = 5; b = 2*a; c = 3*a %% (b) % Comments after a section break d = b + c; % Comments after some code

Notice when you publish the dierence between the line b = 2*a; and the line c = 3*a. As we have set a = 5, we know b = 10, but that result is not printed by the publisher. However, c = 15 is printed. The dierence is from the semicolon, which suppresses output. If you run the code using the Run icon you will notice that c = 15 will print out in the MATLAB command window. When submitting labs, always make sure that any important numerical calculations are shown dont suppress the output with a semicolon but all other code output should be suppressed. Another important thing to notice is how comments immediately after a section break are written dierently than comments after code.

Plotting Data
During labs you will often be asked to create plots. These plots should be included in the section of the lab writeup they are part of. This is easy to do using the MATLAB publisher. Try entering the following code and comments and publishing it. Dont worry if you dont understand what the code is doing, as this will be covered in Lab 1. This exercise is meant only to make you familiar with formatting

your lab writeup. %% Part 2: Plots x = [0 1 2 3 4 5]; y = 2*x; figure(1) plot(x,y) z = x. 2; figure(2) plot(x,z) Notice that both plots appear at the bottom of the section, even though the plot commands are not at the end of this section of code. This is why it is important to separate each section properly, in order that all the gures and plots will appear in the correct places in your writeup.

Calculations by Hand
In some labs, you will be required to show calculations done by hand. If the calculation is short (only a few lines), leave space to write it in by commenting out several blank lines like this: %% Part 3: % Calculations

% The space between the section title and the top (%) is necessary in order to keep the publisher from removing the spaces when it formats the document. Try publishing a section like this. For longer calculations (full page), you may attach a separate page to the end of your lab. Make a note of this in the appropriate section so the grader can easily nd your work.

También podría gustarte