Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.
Comment: Migrated to Confluence 5.3
Include Page
MATLAB Google Analytics
MATLAB Google Analytics
Include Page
Intro Learning Module - Panel
Intro Learning Module - Panel

...

Plot σx vs. r

...

(Take 3: File Input/Output

...

)

Read in [A] from a file

HTML
<iframe width="600" height="338" src="//www.youtube.com/embed/YbyfEIkgYHA?rel=0" frameborder="0" allowfullscreen></iframe>


Read in [B] from the same file

Note that the following video contains a part where the text file is read into Excel. This is only for illustration purposes (to check the range that we need to feed into the dlmread function). The data is still read from the .txt file, so you can skip following the Excel steps and just watch these steps being done in the video.

HTML
<iframe width="600" height="338" src="//www.youtube.com/embed/hdbyuLxkTCg?rel=0" frameborder="0" allowfullscreen></iframe>


Step 5: Plot σx vs. ri, Take 3: File Input/Output

Wiki Markup
Read in \[A\] from a File

Wiki Markup
Instead of specifying _\[A\]_ within the program, let's give the user the option of reading it in from a text file called, say, _input_data.txt_.

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 by searching the help for *dlmread*.

Scroll down to look at the example under Remarks.

Consider the following statement:

A=dlmread('input_data.txt')

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:

Image Removed

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.

Image Removed

Save beam4.m. Run it from the command line.

Image Removed

Does your result look right?

Wiki Markup
Read in \[B\] from a File

Wiki Markup
Hey, if _\[A\]_ can be read in from a file, why not read _\[B\]_ from the same file?

Wiki Markup
Append the elements of _\[B\]_ to _input_data.txt_ and save the file:

Image Removed

Now we need to tell MATLAB to:

  1. Read in the first two rows and assign it to A.
  2. Read in the last two rows and assign it to B.

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
{{\[ (0,0) (0,1) (0,2)...(0,m-1) \]}}

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

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 <span style="color: #660099"><strong><em>dlmread</em></strong></span> command with the delimiter and range inputs to extract both <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 <span style="color: #660099"><strong><em>A</em></strong></span> and {{\[2 0 3 0\]}} for <span style="color: #660099"><strong><em>B</em></strong></span>. Remember to add your semi-colons.

Save and run beam4.m again. You should still get the same results. We can now move on to the next part of the tutorial.

Output Values Using dlmwrite

Just as you can read in values from a text file to a matrix, you can also write a matrix to a text file. The function for this is called dlmwrite. Consider the following line of codesyntax:

Image RemovedImage Added

And the following example:

Image Added

We In the above line of code, we write matrix A to a text file named output_file, and we use a space as a delimiter. Add this line of code to beam4.m and run it. You should see a text file named output_file appear in your working directory. If you wish to learn more about dlmwrite, type help dlmwrite into the command window.use your best friend...documentation!

Output Important Values Using fprintf

Another function that can be used to save important values is fprint f. Suppose we are asked to report the numerical values of the bending stress for inner radius =0.5, 1.0, and 1.5 cm. These values are already contained in the vectors "ri" and "sigma_x".

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 by searching help for fprintfLook up "fprintf" in the documentation for more information. 

The basic form is as follows:

...

fprintf('When ri = %3.2f cm, ',1e2*ri(6));
fprintf('sigma_x = %3.2f MPa\n',sigma_x(6));

Go to Step 6: Plot σx vs. ri, (Take 4: FunctionsSee and rate the complete Learning Module)

Go to all MATLAB Learning Modules