Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

...

Wiki Markup
Create a text file called _input_data.txt_ in your working directory with the elements of _\[A\]_, each row being on a separate line: !input_data.jpg!
Keep in mind that we have used whitespace to delimit (i.e. differentiate) the different numbers. We'll read in this data using the {_}dlmread_ command. Check the documentation on this command:

Help > Index (tab) > dlmread

Scroll down to look at the example under Remarks.

...

Wiki Markup
This will read in _\[A\]_ from the specified file. Let's test this at the command line before we add it to our program:

Check A in the Workspace. Note that each line in the text file is interpreted as a matrix row.

We'll create a new program called beam4.m starting from beam3.m. Select beam3.m in the Editor. Select

Editor > File > Save As

Enter beam4.m for filename. Confirm that you are editing beam4.m. Comment out the original A specification statement and replace it with the above statement using dlmread. Add a semi-colon at the end of the statement.

...

An additional complication is that A and B have different number of columns. To handle this mess, we turn to the following nugget from the dlmread documentation:

Panel

Wiki Markup
M = dlmread('filename', delimiter, range) reads the range specified by range = \[R1 C1 R2 C2\] where (R1,C1) is the upper left corner of the data to be read and (R2,C2) is the lower right corner. You can also specify the range using spreadsheet notation, as in range = 'A1..B7'.

Key point: MATLAB starts counting the rows and colums at 0 rather than 1. So the first row is 0, second row is 1 and so on. The same thing applies to columns also. See below for an input with n rows and m columns. (i, j) are the row, column coordinates.

...

Wiki Markup
{{\[ (2,0) \____________________\_ \]}}

Wiki Markup
{{\[_  ...   \__\____________________\_ \]}}

Wiki Markup
{{\[(n-1,0) \__________\_ \_(n-1,m-1)\]}}

Wiki Markup
Once again, edit _beam4.m_. Now, use the dlmread <span style="color: #660099"><strong><em>dlmread</em></strong></span> command with the delimiter and range inputs to extract both A and B <span style="color: #660099"><strong><em>A</em></strong></span> and <span style="color: #660099"><strong><em>B</em></strong></span> at the same time. The delimiter should be '', the range should be {{\[0 0 1 1\]}} for A <span style="color: #660099"><strong><em>A</em></strong></span> and {{\[2 0 3 0\]}} for B <span style="color: #660099"><strong><em>B</em></strong></span>. Remember to add your semi-colons.

...

The "fprintf" command is useful for outputting information to the command window. You can include both text and numbers in your output and choose the precision of the numbers to match your needs. The documentation for this command can be found at

Help > Index (tab) > fprintf

The basic form is as follows:

fprintf('String with references to variables\n', variables)

The "\n" starts a new line at that point in the statement. Let's take a simple example. Suppose you want to output a number and a percentage with text in between. The variable "sp" is found to be 0.962 and the variable "year" is 2005. Since 2005 is a whole number, this is easy to output using "fprintf" by using "%d". If you are using a decimal number or one that may be inexact, like the percentage, then use "%f". If you put numbers and a decimal between the % and the f, you can control how many number spaces there are before and after the decimal. Let's try it with our example.

>> sp = 0.962;

>> year = 2006;

>> fprintf('In %d, Dave McKee had a %2.2f save percentage playing for Cornell.\n\n', year, sp*100)

This will return the following in the command window:

In 2006, Dave McKee had a 96.20 save percentage playing for Cornell.

Notice that we can perform arthimetic operations in the output section and specify the number of decimals to be 2. Also notice that "\n" switches to a new line and "\t" will produce an indent.

The same concept can be used to output the stress at 0.5, 1.0, and 1.5 cms. We know that since sigma_x increments by 0.1 each time, we can pull out the values of sigma_x corresponding to the correct row numbers (1, 6, and 11). Try this command out and see if it makes sense.

fprintf('The values of the stress are:\n \t %3.2f MPa \t %3.2f MPa \t %3.2f MPa.\n\n', sigma_x(1), sigma_x(6), sigma_x(11));

Before we part, let's remind ourselves of some important programming guidelines that we have followed in this tour:

...