Intro: How to Code and Publish Matlab 2016b to Word (Beginners Guide)
Matlab is a high-performance language program that is used to compute technical results. It has the ability to integrate visuals, computations, and programming in a user-friendly manner. With this program, the user can publish problems and solutions in mathematical notations for others to view.
This instructable will cover some of the basics of Matlab 2016b and publishing your code to Word for others to see. We will begin by introducing you to the overall layout of Matlab and the windows in the program. Next, you will be introduced to variables and how to code them. A couple of problems will be introduced and then you'll finally get to publishing the results of your progress.
These set of instructions are aimed to be simple and to target those who are new to Matlab and its publishing features. An illustration will be provided as well as codes to copy and paste. Keep in mind that you're welcome to play around with and modify the given codes in the steps to get a better understanding of how things work.
Step 1: Matlab's Layout and Windows
The first step is to open up the application and to familiarize the user with the interface. By starting up the program, you'll be introduced to a layout similar to the first screen shot illustrated on this step. Before we begin to start labeling everything, we should open up one more window by clicking on "New Script" on the top left corner. This will open another window for the user to identify.
For this instructable, the user will only need to focus on three particular windows:
The first is boxed in red and will be referred to as the "Script Window" for the upcoming steps. This window allows the user to input multiple lines of codes or commands at once and have them saved, modified, and executed. It is particularly useful for creating a pre-defined function with a series of saved commands to run for later use. The user will learn to write a series of codes such as defining multiple variables at once. (We will go over what a variable is in the next step so don't worry about what it is for now.)
The second window is circled in blue and will be called the "Command Window." This window is used to directly input a single line of code or command for the program to run. This window will provide immediate results for the user to view and to modify. This is where the user will learn to write simple codes such as defining a variable one line at a time. This differs from the "Script Window" in the sense that it only runs one command at a time.
The third window is marked by a green hexagon and is labeled as the "Workspace." This window serves as a book-keeper of all the variables created by the user. By creating a variable, the user can see the organized result in this window. It is used to remain consistent in coding and to avoid creating two of the same variables. This window should be clear whenever the user closes and start up the program so that no variable is saved permanently.
Don't be discouraged if you don't understand what exactly each window does yet from these descriptions. The following steps will have the user write codes followed by illustrations to help simplify everything. Speaking of which, the next step should clarify what a variable is and isn't for the user to utilize for later use.
Step 2: Defining a Variable
A variable in matlab is an element, feature, or factor that is liable to vary or change. It can be a way for the user to identify the letter "a" as a value of any number such as 10. Therefore, when the user calls upon the variable "a," the program will recognize it as a value of 10 instead. Creating one will help in understanding what it is so the next thing to do is to learn how to define one.
To define a variable, there are rules that the user must follow. These rules are:
- Variables must begin with a letter (Keep in mind that variables are case-sensitive)
- Variables must NOT include special characters (such as #, $, %, and etc.)
- Variables can equal another pre-defined variable (pre-defined as in, it was coded in before)
First, we'll illustrate the basics of using the "Command Window" by typing in a few codes you can learn from. Listed below, are variables that follows the rule and are therefore possible options.Try typing each line EXACTLY in the "Command Window" and press enter on your keyboard after each line:
The first illustration provided in this step should be what you get as a result. Notice how in the "Workspace" window, the variables are defined and organized. This is how the user properly defines a variable and utilize their work space.
Also notice how these variables end with a semicolon. These semicolons are necessary to defining variables because it prevents a messy and cluttered "Command Window." The semicolons essentially hide the result of a command, but registers it to the program's "Workspace." The user can try typing the four previous commands without the semicolon and see the result of a messy "Command Window" as shown in the second illustration.
Next, we're going to type the function, "clc"in the "Command Window" and press enter to clear up the messy "Command Window." The user's "Command Window" should be cleared, but if the user desires to recall what a variable is, just type out the variable's name and press enter. For example, the third illustration in this step has the user type "Problem1" and press enter to recall that value.
Another function that the user could use is clearing the work space. This function is executed by having the user type "clear" in the "Command Window." This will remove all defined variables by the user and thus can not have that variable's value be recalled.
The next part of this step is going to teach you the wrong way to define a variable or simply the "dont's." The following variables fail to follow the rules previously stated about defining the variables and will therefore return an error when the user types in the "Command Window":
Notice how in either your result or in the fourth illustration, you can't begin a variable with a number. The rule stated that a variable must begin with a letter and therefore produces an error when not followed. This rule essentially helps the program with syntax or the arrangement of codes.
Now that the user has familiarized themselves with defining variables in the "Command Window" and the "Workspace," the next step will move on to the "Script Window" and have multiple lines be processed at once. This is where things will get tricky, but illustrations and codes will be provided to help with the process.
Step 3: Creating a Script File
A script file was previously defined as a file with a series of codes or commands that could be modified, saved, and executed all at once. For this step, the user will be introduced to some problems and have them performed individually in the "Command Window" and then written in the "Script Window" which is where we'll publish the results in the final step.
1a. Practice Problem
Suppose the user was given a simple algebraic problem and told to solve for Y in the following equation:
There are several ways to solve this problem. We are first going to solve this in the "Command Window" and then transition the codes to the "Script Window." This is so that the user can feel comfortable with defining variables when given a problem before learning to code in the "Script Window."
The solution to our practice problem is to define the givens first and then to define the variable Y as shown in the first illustration and typing the following codes:
Notice that the code ends with a "Y" with no semicolon. The goal here is to recall the value of the variable Y and to have that value visible in the "Command Window." This is essential to follow for the goal of this instructable is to have your results published for others to view. Thus, it is recommended to leave Y with no semicolon even though it is visible to the user in their work space.
Next, the user will be given a set of instructions to solve the exact problem except on a "Script Window." First, type "clear" in the "Command Window" to clear the "Workspace" and then type "clc" to clear the "Command Window." Now proceed to the "Script Window" for the next part of this exercise.
1b. Script Window
In the "Script Window," type the following codes again:
Notice that the when the user presses enter, the variable doesn't appear in the "Workspace." This is because the "Script Window" doesn't execute the codes like the "Command Window" does when a line has been entered. Instead, the "Script Window" allows the user to type multiple lines of code first and then have them all executed at once, saved, and modified. The results should be similar to that of the second illustration provided on this step.
Next, save the file by clicking on "Save" on the editor tab and name the file "Algebra" so that we can execute it. It's important to note that Matlab absolutely refuses to run a script file that isn't saved so be sure to make a habit of this. Also, be sure not to include spaces in the name when you want to create another script file. Matlab won't run a file called, "Algebra Problem" because of the space. The reason behind this is again due to syntax.
Now that the user has saved the file, run the script by clicking on "Run" on the editor tab and the result should appear in the user's "Command Window" and "Workspace." The third illustration on this step should be similar to what the user sees.
1c. Practice Problem 2
This next problem gets a little difficult, but the goal here is to just provide the user with a series of code to copy and eventually publish. So, suppose a teacher asks you to plot a sine wave graph. The solution is again to define variables except this time the user will be introduced to several functions.
Simply, press enter twice after the last command "y" on the "Script Window" and then enter "%%" to create a break in the script file. After that, the user will need to press enter once more and then type, "% Sine Plot." Next, the user will type in these series of code:
The third illustration provides the same series of commands except with comments followed by a percentage sign. These comments are helpful to other users when reviewing published results and highly recommended to experiment with. Also, the string of commands may be hard to follow, but just copy it for now and don't worry about the nature of the coding and their functions. The main goal is to get the user to publish their results.
Save the code and run just like the process done in "1b. Script Window." A graph should pop up to reflect the user's code. Exit the graph window and prepare to publish the results in the next step.
Step 4: Publishing Script File to Word Document
To publish the user's result, click the "Publish" tab near the top left side of the screen and look for the publish feature. The publish feature should have an arrow facing down beneath its icon. Click the arrow beneath the "Publish" feature and click "Edit Publishing Options..." The first illustration will help the user identify where the "Publish" is.
An "Edit Configurations" window will appear on the screen. The next step is then to click on "html" next to the "Output file format" box and to change "html" to "doc." The second illustration will help the user identify these features. Feel free to format the output file to whatever works for publication later on such as PowerPoint for presentation. After the user has selected an output format, click "Publish" on the bottom right corner.
The user will have a Sine Plot graph show up, but after exiting the graph, a word document will appear with the user's codes. The result should be similar to the third illustration provided.
Congrats on completing your published mathematical notation from Matlab!