Matlab: Difference between revisions
From charlesreid1
No edit summary |
No edit summary |
||
| (5 intermediate revisions by the same user not shown) | |||
| Line 14: | Line 14: | ||
|- | |- | ||
|Ones | |Ones | ||
|< | |<pre> | ||
>> ones(3,2); | >> ones(3,2); | ||
</ | </pre> | ||
|<math> | |<math> | ||
\left[ | \left[ | ||
| Line 29: | Line 29: | ||
|- | |- | ||
|Zeros | |Zeros | ||
|< | |<pre> | ||
>> zeros(3,1); | >> zeros(3,1); | ||
</ | </pre> | ||
|<math> | |<math> | ||
\left[ | \left[ | ||
| Line 44: | Line 44: | ||
|- | |- | ||
|Eye (identity) | |Eye (identity) | ||
|< | |<pre> | ||
>> eye(3); | >> eye(3); | ||
</ | </pre> | ||
|<math> | |<math> | ||
\left[ | \left[ | ||
| Line 59: | Line 59: | ||
|- | |- | ||
|Rand (random numbers) | |Rand (random numbers) | ||
|< | |<pre> | ||
>> rand(3,2); | >> rand(3,2); | ||
</ | </pre> | ||
|<math> | |<math> | ||
\left[ | \left[ | ||
| Line 74: | Line 74: | ||
|- | |- | ||
|Meshgrid | |Meshgrid | ||
|< | |<pre> | ||
>> [x,y] = meshgrid(1:4,1:4); | >> [x,y] = meshgrid(1:4,1:4); | ||
</ | </pre> | ||
|<math> | |<math> | ||
x = \left[ | x = \left[ | ||
| Line 104: | Line 104: | ||
(The sum of each row and column is equal to the same value) | (The sum of each row and column is equal to the same value) | ||
|< | |<pre> | ||
>> magic(4); | >> magic(4); | ||
</ | </pre> | ||
|<math> | |<math> | ||
\left[ | \left[ | ||
| Line 120: | Line 120: | ||
|- | |- | ||
|Linspace | |Linspace | ||
|< | |<pre> | ||
>> linspace(A,B,N) | >> linspace(A,B,N) | ||
</ | </pre> | ||
|<math> | |<math> | ||
\left[ | \left[ | ||
| Line 131: | Line 131: | ||
|- | |- | ||
|Logspace | |Logspace | ||
|< | |<pre> | ||
>> logspace(A,B,N) | >> logspace(A,B,N) | ||
</ | </pre> | ||
|<math> | |<math> | ||
\left[ | \left[ | ||
| Line 150: | Line 150: | ||
|Returns the determinant of a matrix: | |Returns the determinant of a matrix: | ||
< | <pre> | ||
>> A=magic(4) | >> A=magic(4) | ||
A = | A = | ||
| Line 161: | Line 161: | ||
>> det(A) | >> det(A) | ||
ans = -1.4495e-12 | ans = -1.4495e-12 | ||
</ | </pre> | ||
|- | |- | ||
|find | |find | ||
| Line 184: | Line 184: | ||
|This function creates a new matrix consisting of several copies of an existing matrix. | |This function creates a new matrix consisting of several copies of an existing matrix. | ||
< | <pre> | ||
>> A = magic(3) | >> A = magic(3) | ||
A = | A = | ||
| Line 201: | Line 201: | ||
3 5 7 3 5 7 | 3 5 7 3 5 7 | ||
4 9 2 4 9 2 | 4 9 2 4 9 2 | ||
</ | </pre> | ||
|- | |- | ||
|size | |size | ||
| Line 216: | Line 216: | ||
Addition/subtraction can be done with vectors or matrices as with numbers: | Addition/subtraction can be done with vectors or matrices as with numbers: | ||
< | <pre> | ||
>> A=ones(2,3) | >> A=ones(2,3) | ||
A = | A = | ||
| Line 240: | Line 240: | ||
0 0 0 | 0 0 0 | ||
0 0 0 | 0 0 0 | ||
</ | </pre> | ||
===Multiplication, division=== | ===Multiplication, division=== | ||
| Line 246: | Line 246: | ||
Multiplication of matrices requires that the inner dimensions must match - i.e. <math>(M \times N) \times (N \times P)</math>. If this criteria is met, then two matrices can be multiplied using normal multiplication syntax. | Multiplication of matrices requires that the inner dimensions must match - i.e. <math>(M \times N) \times (N \times P)</math>. If this criteria is met, then two matrices can be multiplied using normal multiplication syntax. | ||
< | <pre> | ||
>> A | >> A | ||
A = | A = | ||
| Line 267: | Line 267: | ||
0.62476 0.38474 0.86609 | 0.62476 0.38474 0.86609 | ||
1.03284 0.38440 0.64504 | 1.03284 0.38440 0.64504 | ||
</ | </pre> | ||
Division of matrices is defined as <math>A/B = A B^{-1}</math>. The same criteria applies, the dimensions of <math>A</math> must match the dimensions of <math>B^{-1}</math>. If they do, then division can be done using normal division syntax. | Division of matrices is defined as <math>A/B = A B^{-1}</math>. The same criteria applies, the dimensions of <math>A</math> must match the dimensions of <math>B^{-1}</math>. If they do, then division can be done using normal division syntax. | ||
< | <pre> | ||
>> A = rand(3,3) | >> A = rand(3,3) | ||
A = | A = | ||
| Line 302: | Line 302: | ||
0.435077 0.177713 0.788906 | 0.435077 0.177713 0.788906 | ||
</ | </pre> | ||
=== Colon operator === | === Colon operator === | ||
| Line 308: | Line 308: | ||
The colon operator can be used to create a vector, similar to linspace: | The colon operator can be used to create a vector, similar to linspace: | ||
< | <pre> | ||
>> 1:10 | >> 1:10 | ||
ans = | ans = | ||
| Line 314: | Line 314: | ||
1 2 3 4 5 6 7 8 9 10 | 1 2 3 4 5 6 7 8 9 10 | ||
</ | </pre> | ||
The interval between elements can also be specified by using two colons: | The interval between elements can also be specified by using two colons: | ||
< | <pre> | ||
>> (1:0.5:10)' | >> (1:0.5:10)' | ||
ans = | ans = | ||
| Line 358: | Line 358: | ||
9.8000 | 9.8000 | ||
</ | </pre> | ||
The vectors with intervals of 1 can be used to access elements of a vector or a matrix. To access indices M through N, the syntax <code>M:N</code> can be used: | The vectors with intervals of 1 can be used to access elements of a vector or a matrix. To access indices M through N, the syntax <code>M:N</code> can be used: | ||
< | <pre> | ||
>> A = magic(4) | >> A = magic(4) | ||
| Line 378: | Line 378: | ||
5 11 | 5 11 | ||
</ | </pre> | ||
The colon operator by itself can also indicate an index ranging the entire length of the vector or matrix: | The colon operator by itself can also indicate an index ranging the entire length of the vector or matrix: | ||
< | <pre> | ||
>> A(1,:) | >> A(1,:) | ||
| Line 389: | Line 389: | ||
16 2 3 13 | 16 2 3 13 | ||
</ | </pre> | ||
== Component-wise operators == | == Component-wise operators == | ||
| Line 411: | Line 411: | ||
This component-wise operation can be done in Matlab by putting a dot in front of the operator: <math>.\lozenge</math> | This component-wise operation can be done in Matlab by putting a dot in front of the operator: <math>.\lozenge</math> | ||
< | <pre> | ||
>> A | >> A | ||
A = | A = | ||
| Line 440: | Line 440: | ||
1.99626 1.08044 7.36944 | 1.99626 1.08044 7.36944 | ||
</ | </pre> | ||
However, if a component-wise operator operates on two vectors or matrices, the vectors or matrices must be the same size. Otherwise, the operator will not work. | However, if a component-wise operator operates on two vectors or matrices, the vectors or matrices must be the same size. Otherwise, the operator will not work. | ||
| Line 446: | Line 446: | ||
This can also be done with exponential operators: | This can also be done with exponential operators: | ||
< | <pre> | ||
>> A=rand(4,1)*10 | >> A=rand(4,1)*10 | ||
| Line 464: | Line 464: | ||
37.074953 | 37.074953 | ||
</ | </pre> | ||
=== Functions === | === Functions === | ||
| Line 470: | Line 470: | ||
If a vector or matrix is fed to a built-in Matlab function such as <code>sin()</code> or <code>exp()</code>, the function operates component-wise on the vector or matrix. For example: | If a vector or matrix is fed to a built-in Matlab function such as <code>sin()</code> or <code>exp()</code>, the function operates component-wise on the vector or matrix. For example: | ||
< | <pre> | ||
>> x = ( 0:pi/4:2*pi )' | >> x = ( 0:pi/4:2*pi )' | ||
x = | x = | ||
| Line 496: | Line 496: | ||
-0.70711 | -0.70711 | ||
-0.00000 | -0.00000 | ||
</ | </pre> | ||
Combined with the colon operator or linspace function, this provides a very easy way to evaluate a function at many points. | Combined with the colon operator or linspace function, this provides a very easy way to evaluate a function at many points. | ||
| Line 502: | Line 502: | ||
Meshgrid can also be used to evaluate a function of two variables, in a form that is convenient to plot: | Meshgrid can also be used to evaluate a function of two variables, in a form that is convenient to plot: | ||
< | <pre> | ||
>> [x,y] = meshgrid(0:pi/4:2*pi, 0:pi/4:2*pi); | >> [x,y] = meshgrid(0:pi/4:2*pi, 0:pi/4:2*pi); | ||
>> z = x .* sin( x - y ); | >> z = x .* sin( x - y ); | ||
</ | </pre> | ||
This results in a set of 3 matrices that are particularly convenient to plot using <code>surf</code> or <code>contourf</code> (more on these plotting functions below). | This results in a set of 3 matrices that are particularly convenient to plot using <code>surf</code> or <code>contourf</code> (more on these plotting functions below). | ||
< | <pre> | ||
>> surf(x,y,z) | >> surf(x,y,z) | ||
</ | </pre> | ||
= Input/output = | = Input/output = | ||
| Line 527: | Line 527: | ||
This can be done using either a GUI interface: | This can be done using either a GUI interface: | ||
< | <pre> | ||
>>> directory_name = uigetdir('/path/to/starting/point','Pick a directory:'); | >>> directory_name = uigetdir('/path/to/starting/point','Pick a directory:'); | ||
</ | </pre> | ||
once you pick a directory, it is of type <code>char</code>: | once you pick a directory, it is of type <code>char</code>: | ||
< | <pre> | ||
>>> directoryname = uigetdir('/path/to/starting/point','Pick a directory:'); | >>> directoryname = uigetdir('/path/to/starting/point','Pick a directory:'); | ||
| Line 542: | Line 542: | ||
char | char | ||
</ | </pre> | ||
Alternatively, since the directory name simply needs to be stored as a char, you can hard-code your own directory: | Alternatively, since the directory name simply needs to be stored as a char, you can hard-code your own directory: | ||
< | <pre> | ||
>> directoryname='/uufs/chpc.utah.edu/common/home/u0552682'; | >> directoryname='/uufs/chpc.utah.edu/common/home/u0552682'; | ||
| Line 554: | Line 554: | ||
char | char | ||
</ | </pre> | ||
You can also specify particular files, or file-matching patterns, as part of the path: | You can also specify particular files, or file-matching patterns, as part of the path: | ||
< | <pre> | ||
>> directoryname='/uufs/chpc.utah.edu/common/home/u0552682/files/*.txt'; | >> directoryname='/uufs/chpc.utah.edu/common/home/u0552682/files/*.txt'; | ||
</ | </pre> | ||
===Get the directory listing=== | ===Get the directory listing=== | ||
< | <pre> | ||
>> ls = dir(directoryname) | >> ls = dir(directoryname) | ||
| Line 582: | Line 582: | ||
struct | struct | ||
</ | </pre> | ||
The elements of this struct can be accessed as follows: | The elements of this struct can be accessed as follows: | ||
< | <pre> | ||
>> ls(1) | >> ls(1) | ||
| Line 606: | Line 606: | ||
isdir: 0 | isdir: 0 | ||
datenum: 7.3464e+05 | datenum: 7.3464e+05 | ||
</ | </pre> | ||
Individual fields of each struct element can be accessed like this: | Individual fields of each struct element can be accessed like this: | ||
< | <pre> | ||
ls(1).name | ls(1).name | ||
| Line 623: | Line 623: | ||
i002_j072_k072.dat | i002_j072_k072.dat | ||
</ | </pre> | ||
===Get the directory listing of interesting files=== | ===Get the directory listing of interesting files=== | ||
| Line 629: | Line 629: | ||
To exclude uninteresting entries like "." and "..", you can also use file-matching patterns: | To exclude uninteresting entries like "." and "..", you can also use file-matching patterns: | ||
< | <pre> | ||
>> directoryname = '/uufs/chpc.utah.edu/common/home/u0552682/files/*.txt'; | >> directoryname = '/uufs/chpc.utah.edu/common/home/u0552682/files/*.txt'; | ||
| Line 642: | Line 642: | ||
isdir | isdir | ||
datenum | datenum | ||
</ | </pre> | ||
===Looping over files in directory=== | ===Looping over files in directory=== | ||
| Line 648: | Line 648: | ||
You can now loop through the directory listing, which is stored in the structure <code>ls</code>, and load the file if it is not a directory: | You can now loop through the directory listing, which is stored in the structure <code>ls</code>, and load the file if it is not a directory: | ||
< | <pre> | ||
for d=1:length(ls) | for d=1:length(ls) | ||
if ~ls(d).isdir | if ~ls(d).isdir | ||
| Line 656: | Line 656: | ||
end | end | ||
end | end | ||
</ | </pre> | ||
===Opening/loading files=== | ===Opening/loading files=== | ||
| Line 662: | Line 662: | ||
This is something that will be specific to your own file format. You'll want to read through the Matlab documentation for the <code>textscan</code> function [http://www.mathworks.com/help/techdoc/ref/textscan.html], or if your text files are simpler you will probably want to just use the simpler <code>load</code> function [http://www.mathworks.com/help/techdoc/ref/load.html]. | This is something that will be specific to your own file format. You'll want to read through the Matlab documentation for the <code>textscan</code> function [http://www.mathworks.com/help/techdoc/ref/textscan.html], or if your text files are simpler you will probably want to just use the simpler <code>load</code> function [http://www.mathworks.com/help/techdoc/ref/load.html]. | ||
< | <pre> | ||
fid=fopen(filename); | fid=fopen(filename); | ||
AllData{d}=textscan(fid,'%f %f %f %f %f %f %f %f %f %f','CommentStyle','#'); | AllData{d}=textscan(fid,'%f %f %f %f %f %f %f %f %f %f','CommentStyle','#'); | ||
fclose(fid); | fclose(fid); | ||
</ | </pre> | ||
This loads a file that consists of 10 numbers, separated with spaces, and with all comment lines commented with a hash symbol. | This loads a file that consists of 10 numbers, separated with spaces, and with all comment lines commented with a hash symbol. | ||
| Line 682: | Line 682: | ||
To save a variable 'really_big_variable' to an ASCII file, | To save a variable 'really_big_variable' to an ASCII file, | ||
< | <pre> | ||
save('really_big_variable.txt', 'really_big_variable', '-ASCII'); | save('really_big_variable.txt', 'really_big_variable', '-ASCII'); | ||
</ | </pre> | ||
On the other hand, the MAT format is a binary format, meaning it is more compressed, but not as straightforward for other programs to read. | On the other hand, the MAT format is a binary format, meaning it is more compressed, but not as straightforward for other programs to read. | ||
| Line 690: | Line 690: | ||
To save a variable in MAT format: | To save a variable in MAT format: | ||
< | <pre> | ||
save('really_big_variable.mat', 'really_big_variable', '-MAT'); | save('really_big_variable.mat', 'really_big_variable', '-MAT'); | ||
</ | </pre> | ||
| Line 723: | Line 723: | ||
== Statistics == | == Statistics == | ||
--> | --> | ||
=Matlab Object Programming= | |||
http://www.mathworks.com/help/techdoc/matlab_oop/ug_intropage.html | |||
"The object-oriented programming capabilities of the MATLAB language enable you to develop complex technical computing applications faster than with other languages, such as C++, C#, and Java." | |||
This is very true. | |||
==Matlab Classes== | |||
Using the <code>whos</code> command, you can see types of objects | |||
alt, use <code>class(variable)</code>, e.g.: | |||
<pre> | |||
>>> a = 5+5; | |||
>>> class(a) | |||
ans = | |||
double | |||
</pre> | |||
=See also= | =See also= | ||
| Line 731: | Line 757: | ||
* [[Octave]]: http://www.gnu.org/software/octave/ | * [[Octave]]: http://www.gnu.org/software/octave/ | ||
{{Programs}} | {{Programs}} | ||
{{ | {{ScientificComputingFlag}} | ||
{{LinearAlgebraFlag}} | |||
Latest revision as of 09:22, 16 April 2017
Matrices
Basics
See the Introduction to Matlab lecture.
Special matrices/vectors
| Name (matrix type) | Matlab syntax | Result |
|---|---|---|
| Ones | >> ones(3,2); |
$ \left[ \begin{array}{cc} 1 & 1 \\ 1 & 1 \\ 1 & 1 \end{array} \right] $ |
| Zeros | >> zeros(3,1); |
$ \left[ \begin{array}{cc} 0 \\ 0 \\ 0 \end{array} \right] $ |
| Eye (identity) | >> eye(3); |
$ \left[ \begin{array}{ccc} 1 & 0 & 0 \\ 0 & 1 & 0 \\ 0 & 0 & 1 \end{array} \right] $ |
| Rand (random numbers) | >> rand(3,2); |
$ \left[ \begin{array}{cc} 0.21955 & 0.27560\\ 0.42385 & 0.62212\\ 0.53343 & 0.69182 \end{array} \right] $ |
| Meshgrid | >> [x,y] = meshgrid(1:4,1:4); |
$ x = \left[ \begin{array}{cccc} 1& 2& 3& 4\\ 1& 2& 3& 4\\ 1& 2& 3& 4\\ 1& 2& 3& 4 \end{array} \right] $
$ y = \left[ \begin{array}{cccc} 1& 1& 1& 1\\ 2& 2& 2& 2\\ 3& 3& 3& 3\\ 4& 4& 4& 4 \end{array} \right] $ |
| Magic (magic square matrix)
(The sum of each row and column is equal to the same value) |
>> magic(4); |
$ \left[ \begin{array}{cccc} 16& 2& 3& 13\\ 5& 11& 10& 8\\ 9& 7& 6& 12\\ 4& 14& 15& 1 \end{array} \right] $ |
| Linspace | >> linspace(A,B,N) |
$ \left[ A + \left( \frac{i}{N-1} \right) B \right] \qquad i=0, \dots, N-1 $ |
| Logspace | >> logspace(A,B,N) |
$ \left[ 10^{A} + 10^{ \left( \frac{i}{N-1} \right) B } \right] \qquad i=0, \dots, N-1 $ |
Functions
| Function name | Matlab syntax/output |
|---|---|
| det | Returns the determinant of a matrix:
>> A=magic(4)
A =
16 2 3 13
5 11 10 8
9 7 6 12
4 14 15 1
>> det(A)
ans = -1.4495e-12
|
| find | |
| flipud | |
| fliplr | |
| length | |
| max | |
| min | |
| repmat | This function creates a new matrix consisting of several copies of an existing matrix.
>> A = magic(3) A = 8 1 6 3 5 7 4 9 2 >> repmat(A,2,2) ans = 8 1 6 8 1 6 3 5 7 3 5 7 4 9 2 4 9 2 8 1 6 8 1 6 3 5 7 3 5 7 4 9 2 4 9 2 |
| size | |
| sort |
Matrix operators
Addition, subtraction
Addition/subtraction can be done with vectors or matrices as with numbers:
>> A=ones(2,3) A = 1 1 1 1 1 1 >> B=ones(2,3) B = 1 1 1 1 1 1 >> C = A + B C = 2 2 2 2 2 2 >> C = A - B C = 0 0 0 0 0 0
Multiplication, division
Multiplication of matrices requires that the inner dimensions must match - i.e. $ (M \times N) \times (N \times P) $. If this criteria is met, then two matrices can be multiplied using normal multiplication syntax.
>> A A = 0.85645 0.86793 0.39228 0.22329 0.82611 0.40042 0.79097 0.45921 0.30861 >> B B = 0.976938 0.200895 0.239939 0.300156 0.205414 0.963250 0.396226 0.425022 0.041877 >> C = A*B C = 1.25264 0.51707 1.05796 0.62476 0.38474 0.86609 1.03284 0.38440 0.64504
Division of matrices is defined as $ A/B = A B^{-1} $. The same criteria applies, the dimensions of $ A $ must match the dimensions of $ B^{-1} $. If they do, then division can be done using normal division syntax.
>> A = rand(3,3) A = 0.85645 0.86793 0.39228 0.22329 0.82611 0.40042 0.79097 0.45921 0.30861 >> B = rand(3,3) B = 0.976938 0.200895 0.239939 0.300156 0.205414 0.963250 0.396226 0.425022 0.041877 >> >> C = A/B C = 0.015664 0.321640 1.879233 -0.763591 0.516569 2.054946 0.435077 0.177713 0.788906 >> C = A*inv(B) C = 0.015664 0.321640 1.879233 -0.763591 0.516569 2.054946 0.435077 0.177713 0.788906
Colon operator
The colon operator can be used to create a vector, similar to linspace:
>> 1:10
ans =
1 2 3 4 5 6 7 8 9 10
The interval between elements can also be specified by using two colons:
>> (1:0.5:10)'
ans =
1.0000
1.5000
2.0000
2.5000
3.0000
3.5000
4.0000
4.5000
5.0000
5.5000
6.0000
6.5000
7.0000
7.5000
8.0000
8.5000
9.0000
9.5000
10.0000
>> (1:0.8:10)'
ans =
1.0000
1.8000
2.6000
3.4000
4.2000
5.0000
5.8000
6.6000
7.4000
8.2000
9.0000
9.8000
The vectors with intervals of 1 can be used to access elements of a vector or a matrix. To access indices M through N, the syntax M:N can be used:
>> A = magic(4)
A =
16 2 3 13
5 11 10 8
9 7 6 12
4 14 15 1
>> A(1:2,1:2)
ans =
16 2
5 11
The colon operator by itself can also indicate an index ranging the entire length of the vector or matrix:
>> A(1,:) ans = 16 2 3 13
Component-wise operators
Component-wise multiplication and division can also be done. For two vectors $ a_{i}, b_{j} $ or two matrices $ A_{i,j}, B_{m,n} $ and some arbitrary operator $ \lozenge $, the component-wise vector operation is defined as
$ \begin{array}{rcl} c_{k} &=& a_{k} \, \lozenge \, b_{k} \end{array} $
and the component-wise matrix operation is defined as
$ \begin{array}{rcl} C_{p,q} &=& A_{p,q} \, \lozenge \, B_{p,q} \end{array} $
This component-wise operation can be done in Matlab by putting a dot in front of the operator: $ .\lozenge $
>> A A = 0.85645 0.86793 0.39228 0.22329 0.82611 0.40042 0.79097 0.45921 0.30861 >> B B = 0.976938 0.200895 0.239939 0.300156 0.205414 0.963250 0.396226 0.425022 0.041877 >> C = A.*B C = 0.836694 0.174363 0.094122 0.067023 0.169693 0.385709 0.313402 0.195175 0.012924 >> C = A./B C = 0.87666 4.32032 1.63489 0.74392 4.02167 0.41570 1.99626 1.08044 7.36944
However, if a component-wise operator operates on two vectors or matrices, the vectors or matrices must be the same size. Otherwise, the operator will not work.
This can also be done with exponential operators:
>> A=rand(4,1)*10
A =
5.91734
0.22397
8.80927
6.08892
>> A.^2
ans =
35.014866
0.050161
77.603268
37.074953
Functions
If a vector or matrix is fed to a built-in Matlab function such as sin() or exp(), the function operates component-wise on the vector or matrix. For example:
>> x = ( 0:pi/4:2*pi )' x = 0.00000 0.78540 1.57080 2.35619 3.14159 3.92699 4.71239 5.49779 6.28319 >> sin(x) ans = 0.00000 0.70711 1.00000 0.70711 0.00000 -0.70711 -1.00000 -0.70711 -0.00000
Combined with the colon operator or linspace function, this provides a very easy way to evaluate a function at many points.
Meshgrid can also be used to evaluate a function of two variables, in a form that is convenient to plot:
>> [x,y] = meshgrid(0:pi/4:2*pi, 0:pi/4:2*pi); >> z = x .* sin( x - y );
This results in a set of 3 matrices that are particularly convenient to plot using surf or contourf (more on these plotting functions below).
>> surf(x,y,z)
Input/output
Reading Data Files
You can read in data files with the textscan function.
Batch File Loading
You can load a batch of files (say, all the files in a given directory) as follows.
Pick a directory
This can be done using either a GUI interface:
>>> directory_name = uigetdir('/path/to/starting/point','Pick a directory:');
once you pick a directory, it is of type char:
>>> directoryname = uigetdir('/path/to/starting/point','Pick a directory:');
>>> class(directoryname)
ans =
char
Alternatively, since the directory name simply needs to be stored as a char, you can hard-code your own directory:
>> directoryname='/uufs/chpc.utah.edu/common/home/u0552682'; >> class(directoryname) ans = char
You can also specify particular files, or file-matching patterns, as part of the path:
>> directoryname='/uufs/chpc.utah.edu/common/home/u0552682/files/*.txt';
Get the directory listing
>> ls = dir(directoryname)
ls =
146x1 struct array with fields:
name
date
bytes
isdir
datenum
>> class(ls)
ans =
struct
The elements of this struct can be accessed as follows:
>> ls(1)
ans =
name: '.'
date: '28-May-2011 00:04:50'
bytes: 0
isdir: 1
datenum: 7.3465e+05
>> ls(5)
ans =
name: 'i002_j072_k072.dat'
date: '14-May-2011 17:50:10'
bytes: 1002884
isdir: 0
datenum: 7.3464e+05
Individual fields of each struct element can be accessed like this:
ls(1).name ans = . >> ls(5).name ans = i002_j072_k072.dat
Get the directory listing of interesting files
To exclude uninteresting entries like "." and "..", you can also use file-matching patterns:
>> directoryname = '/uufs/chpc.utah.edu/common/home/u0552682/files/*.txt';
>> ls = dir(directoryname)
ls =
26x1 struct array with fields:
name
date
bytes
isdir
datenum
Looping over files in directory
You can now loop through the directory listing, which is stored in the structure ls, and load the file if it is not a directory:
for d=1:length(ls)
if ~ls(d).isdir
filename = fullfile('/absolute/path/to/files/',ls(d).name]);
% open/load the file here
end
end
Opening/loading files
This is something that will be specific to your own file format. You'll want to read through the Matlab documentation for the textscan function [1], or if your text files are simpler you will probably want to just use the simpler load function [2].
fid=fopen(filename);
AllData{d}=textscan(fid,'%f %f %f %f %f %f %f %f %f %f','CommentStyle','#');
fclose(fid);
This loads a file that consists of 10 numbers, separated with spaces, and with all comment lines commented with a hash symbol.
Saving Data to a File
If you have variables in your workspace that are processor-intensive to calculate, and that you want to be able to load many times in the future, you can save your workspace rather than re-calculating the variables every time.
The save() command will save workspace variables to a file.
Ascii vs. Mat format
The ASCII format will take up a lot more space, because it's saving everything out in human-readable ASCII format. This is useful if, say, you want other programs to read Matlab's saved workspace variables.
To save a variable 'really_big_variable' to an ASCII file,
save('really_big_variable.txt', 'really_big_variable', '-ASCII');
On the other hand, the MAT format is a binary format, meaning it is more compressed, but not as straightforward for other programs to read.
To save a variable in MAT format:
save('really_big_variable.mat', 'really_big_variable', '-MAT');
File I/O References
- Save(): http://www.mathworks.com/help/techdoc/ref/save.html
- Exist(): http://www.mathworks.com/help/techdoc/ref/exist.html
- Load(): http://www.mathworks.com/help/techdoc/ref/load.html
- Textscan(): http://www.mathworks.com/help/techdoc/ref/textscan.html
- Loading files in a directory: http://answers.yahoo.com/question/index?qid=20090118182802AAn1D6n
Matlab Object Programming
http://www.mathworks.com/help/techdoc/matlab_oop/ug_intropage.html
"The object-oriented programming capabilities of the MATLAB language enable you to develop complex technical computing applications faster than with other languages, such as C++, C#, and Java."
This is very true.
Matlab Classes
Using the whos command, you can see types of objects
alt, use class(variable), e.g.:
>>> a = 5+5; >>> class(a) ans = double
See also
- Introduction to Matlab (lecture)
References
| Scientific Computing Topics in scientific computing.
Numerical Software: Lapack · Sundials · Matlab · Octave · FFTW Petsc · Example Petsc Makefile · Trilinos · Hypre · Ginac · Gnuplot
Python: Numpy · Scipy · Pandas · Matplotlib · Python Sundials · Py4Sci Scikit-learn: Sklearn · Skimage
|
| Linear Algebra Topics in linear algebra.
Matlab · Octave · Sundials · Trilinos
|