help fread
fread Read binary data from file.
A = fread(FID) reads binary data from the specified file
and writes it into matrix A. FID is an integer file
identifier obtained from FOPEN. MATLAB reads the entire
file and positions the file pointer at the end of the file
(see FEOF for details).
A = fread(FID,SIZE) reads the number of elements specified
by SIZE. Valid entries for SIZE are:
N read N elements into a column vector.
inf read to the end of the file.
[M,N] read elements to fill an M-by-N matrix, in column order.
N can be inf, but M can't.
A = fread(FID,SIZE,PRECISION) reads the file according to
the data format specified by the string PRECISION. The
PRECISION input commonly contains a datatype specifier like
'int' or 'float', followed by an integer giving the size in
bits. The SIZE argument is optional when using this syntax.
Any of the following strings, either the MATLAB version, or
their C or Fortran equivalent, may be used. If not specified,
the default precision is 'uint8'.
MATLAB C or Fortran Description
'uchar' 'unsigned char' unsigned integer, 8 bits.
'schar' 'signed char' signed integer, 8 bits.
'int8' 'integer*1' integer, 8 bits.
'int16' 'integer*2' integer, 16 bits.
'int32' 'integer*4' integer, 32 bits.
'int64' 'integer*8' integer, 64 bits.
'uint8' 'integer*1' unsigned integer, 8 bits.
'uint16' 'integer*2' unsigned integer, 16 bits.
'uint32' 'integer*4' unsigned integer, 32 bits.
'uint64' 'integer*8' unsigned integer, 64 bits.
'single' 'real*4' floating point, 32 bits.
'float32' 'real*4' floating point, 32 bits.
'double' 'real*8' floating point, 64 bits.
'float64' 'real*8' floating point, 64 bits.
The following platform dependent formats are also supported but
they are not guaranteed to be the same size on all platforms.
MATLAB C or Fortran Description
'char' 'char*1' character.
'short' 'short' integer, 16 bits.
'int' 'int' integer, 32 bits.
'long' 'long' integer, 32 or 64 bits.
'ushort' 'unsigned short' unsigned integer, 16 bits.
'uint' 'unsigned int' unsigned integer, 32 bits.
'ulong' 'unsigned long' unsigned integer, 32 bits or 64 bits.
'float' 'float' floating point, 32 bits.
If the precision is 'char' or 'char*1', MATLAB reads characters
using the encoding scheme associated with the file. See FOPEN
for more information.
The following formats map to an input stream of bits rather than
bytes.
'bitN' signed integer, N bits (1<=N<=64).
'ubitN' unsigned integer, N bits (1<=N<=64).
If the input stream is bytes and fread reaches the end of file
(see FEOF) in the middle of reading the number of bytes required
for an element, the partial result is ignored. However, if the
input stream is bits, then the partial result is returned as the
last value. If an error occurs before reaching the end of file,
only full elements read up to that point are used.
By default, numeric and character values are returned in class
'double' arrays. To return these values stored in classes other
than double, create your PRECISION argument by first specifying
your source format, then following it by '=>', and finally
specifying your destination format. If the source and destination
formats are the same then the following shorthand notation may be
used:
*source
which means:
source=>source
For example,
uint8=>uint8 read in unsigned 8-bit integers and
save them in an unsigned 8-bit integer
array
*uint8 shorthand version of previous example
bit4=>int8 read in signed 4-bit integers packed
in bytes and save them in a signed
8-bit integer array (each 4-bit
integer becomes one 8-bit integer)
double=>real*4 read in doubles, convert and save
as a 32-bit floating point array
A = fread(FID,SIZE,PRECISION,SKIP) includes a SKIP argument that
specifies the number of bytes to skip after each PRECISION value
is read. If PRECISION specifies a bit source format, like 'bitN' or
'ubitN', the SKIP argument is interpreted as the number of bits to
skip. The SIZE argument is optional when using this syntax.
When SKIP is used, the PRECISION string may contain a positive
integer repetition factor of the form 'N*' which prepends the source
format of the PRECISION argument, like '40*uchar'. Note that 40*uchar
for the PRECISION alone is equivalent to '40*uchar=>double', not
'40*uchar=>uchar'. With SKIP specified, fread reads in, at most, a
repetition factor number of values (default of 1), does a skip of
input specified by the SKIP argument, reads in another block of values
and does a skip of input, etc. until SIZE number of values have been
read. If a SKIP argument is not specified, the repetition factor is
ignored. Repetition with skip is useful for extracting data in
noncontiguous fields from fixed length records.
For example,
s = fread(fid,120,'40*uchar=>uchar',8);
reads in 120 characters in blocks of 40 each separated by 8
characters.
A = fread(FID,SIZE,PRECISION,SKIP,MACHINEFORMAT) treats the data
read as having a format given by the string MACHINEFORMAT. You
can obtain the MACHINEFORMAT argument from the output of the
FOPEN function. See FOPEN for possible values for MACHINEFORMAT.
The SIZE and SKIP arguments are optional when using this syntax.
[A, COUNT] = fread(...) Optional output argument COUNT returns
the number of elements successfully read.
Examples:
The file alphabet.txt contains the 26 letters of the English
alphabet, all capitalized. Open the file for read access with
fopen, and read the first five elements into output c. Because
a precision has not been specified, MATLAB uses the default
precision of uchar, and the output is numeric:
fid = fopen('alphabet.txt', 'r');
c = fread(fid, 5)'
c =
65 66 67 68 69
fclose(fid);
This time, specify that you want each element read as an unsigned
8-bit integer and output as a character. (Using a precision of
'char=>char' or '*char' will produce the same result):
fid = fopen('alphabet.txt', 'r');
c = fread(fid, 5, 'uint8=>char')'
c =
ABCDE
fclose(fid);
See also fwrite, fseek, fscanf, fgetl, fgets, load, fopen, feof.
Overloaded methods:
serial/fread
udp/fread
i2c/fread
icinterface/fread
Reference page in Help browser
doc fread
help fscanf
fscanf Read data from text file.
[A,COUNT] = fscanf(FID,FORMAT) reads and converts data from a text file
into array A in column order. FID is a file identifier obtained from
FOPEN. COUNT is an optional output argument that returns the number of
elements successfully read.
FORMAT is a string containing ordinary characters and/or conversion
specifications, which include a % character, an optional asterisk for
assignment suppression, an optional width field, and a conversion
character (such as d, i, o, u, x, e, f, g, s, or c).
fscanf reapplies the FORMAT throughout the entire file. If fscanf
cannot match the FORMAT to the data, it reads only the portion that
matches into A and then stops processing. For more details on the
FORMAT input, type "doc fscanf" at the command prompt.
[A,COUNT] = fscanf(FID,FORMAT,SIZEA) reads SIZEA elements into A.
Valid forms for SIZEA are:
inf Read to the end of the file. (default)
N Read at most N elements into a column vector.
[M,N] Read at most M * N elements filling at least an M-by-N matrix
in column order. N can be inf, but M cannot.
Notes:
MATLAB reads characters using the encoding scheme associated with
the file. See FOPEN for more information. MATLAB converts characters in
both the FORMAT string and in the file to the internal character
representation before comparing.
Examples:
File count.dat contains three columns of integers. Read the values in
column order, and transpose to match the appearance of the file:
fid = fopen('count.dat');
A = fscanf(fid,'%d',[3,inf])';
fclose(fid);
Read the contents of plus.m into a character string:
fid = fopen('plus.m');
S = fscanf(fid,'%s');
fclose(fid);
See also fopen, fprintf, sscanf, textscan, fgetl, fgets, fread, input.
Overloaded methods:
serial/fscanf
udp/fscanf
i2c/fscanf
icinterface/fscanf
Reference page in Help browser
doc fscanf
datafile = fopen('data.txt', 'r');
fscanf(datafile, '%d\t%f\t%f\n', [3 inf])
ans =
Columns 1 through 8
1.000000000000000 2.000000000000000 3.000000000000000 4.000000000000000 5.000000000000000 6.000000000000000 7.000000000000000 8.000000000000000
3.278703000000000 0.178558000000000 4.245647000000000 4.669966000000000 3.393676000000000 3.788701000000000 3.715662000000000 1.961135000000000
9.728410999999999 13.471447000000000 17.417534000000000 26.034822999999999 10.175221000000001 13.311718000000001 11.216825999999999 8.665321000000000
Columns 9 through 16
9.000000000000000 10.000000000000000 11.000000000000000 12.000000000000000 13.000000000000000 14.000000000000000 15.000000000000000 16.000000000000000
3.277389000000000 0.855933000000000 3.530230000000000 0.159164000000000 1.384615000000000 0.230857000000000 0.485659000000000 4.117289000000000
7.864211000000000 13.326117000000000 12.154336000000001 13.898421000000001 13.355270000000001 14.476838000000001 15.766473000000000 14.740531000000001
Columns 17 through 24
17.000000000000000 18.000000000000000 19.000000000000000 20.000000000000000 21.000000000000000 22.000000000000000 23.000000000000000 24.000000000000000
3.474143000000000 1.585497000000000 4.751110000000000 0.172230000000000 2.193722000000000 1.907792000000000 3.827584000000000 3.976000000000000
8.077714000000000 10.336230000000000 27.874434000000001 14.368339000000001 9.073857000000000 11.795859000000000 12.425557000000000 13.913823000000001
Columns 25 through 32
25.000000000000000 26.000000000000000 27.000000000000000 28.000000000000000 29.000000000000000 30.000000000000000 31.000000000000000 32.000000000000000
0.934363000000000 2.448822000000000 2.227931000000000 3.231565000000000 3.546824000000000 3.773433000000000 1.380125000000000 3.398513000000000
15.775710999999999 7.692099000000000 9.848694999999999 9.293559999999999 9.662685000000000 11.844568000000001 11.459716000000000 7.949101000000000
Columns 33 through 40
33.000000000000000 34.000000000000000 35.000000000000000 36.000000000000000 37.000000000000000 38.000000000000000 39.000000000000000 40.000000000000000
3.275490000000000 0.813059000000000 0.594988000000000 2.491820000000000 4.798720000000000 1.701929000000000 2.926339000000000 1.119060000000000
8.704852000000001 15.206651000000001 17.406020999999999 7.726680000000000 28.747188000000001 11.172155000000000 6.014620000000000 13.177192000000000
fscanf(datafile, '%d\t%f\t%f\n', [3 inf])'
ans =
[]
fclose(datafile);
datafile = fopen('data.txt', 'r');
data = fscanf(datafile, '%d\t%f\t%f\n', [3 inf])';
whos(data)
{Error using whos
Argument must contain a string.
}
whos('data')
Name Size Bytes Class Attributes
data 40x3 960 double
data
data =
1.000000000000000 3.278703000000000 9.728410999999999
2.000000000000000 0.178558000000000 13.471447000000000
3.000000000000000 4.245647000000000 17.417534000000000
4.000000000000000 4.669966000000000 26.034822999999999
5.000000000000000 3.393676000000000 10.175221000000001
6.000000000000000 3.788701000000000 13.311718000000001
7.000000000000000 3.715662000000000 11.216825999999999
8.000000000000000 1.961135000000000 8.665321000000000
9.000000000000000 3.277389000000000 7.864211000000000
10.000000000000000 0.855933000000000 13.326117000000000
11.000000000000000 3.530230000000000 12.154336000000001
12.000000000000000 0.159164000000000 13.898421000000001
13.000000000000000 1.384615000000000 13.355270000000001
14.000000000000000 0.230857000000000 14.476838000000001
15.000000000000000 0.485659000000000 15.766473000000000
16.000000000000000 4.117289000000000 14.740531000000001
17.000000000000000 3.474143000000000 8.077714000000000
18.000000000000000 1.585497000000000 10.336230000000000
19.000000000000000 4.751110000000000 27.874434000000001
20.000000000000000 0.172230000000000 14.368339000000001
21.000000000000000 2.193722000000000 9.073857000000000
22.000000000000000 1.907792000000000 11.795859000000000
23.000000000000000 3.827584000000000 12.425557000000000
24.000000000000000 3.976000000000000 13.913823000000001
25.000000000000000 0.934363000000000 15.775710999999999
26.000000000000000 2.448822000000000 7.692099000000000
27.000000000000000 2.227931000000000 9.848694999999999
28.000000000000000 3.231565000000000 9.293559999999999
29.000000000000000 3.546824000000000 9.662685000000000
30.000000000000000 3.773433000000000 11.844568000000001
31.000000000000000 1.380125000000000 11.459716000000000
32.000000000000000 3.398513000000000 7.949101000000000
33.000000000000000 3.275490000000000 8.704852000000001
34.000000000000000 0.813059000000000 15.206651000000001
35.000000000000000 0.594988000000000 17.406020999999999
36.000000000000000 2.491820000000000 7.726680000000000
37.000000000000000 4.798720000000000 28.747188000000001
38.000000000000000 1.701929000000000 11.172155000000000
39.000000000000000 2.926339000000000 6.014620000000000
40.000000000000000 1.119060000000000 13.177192000000000
plot(data(:,[2 3]),'+b')
plot(data(:,2),data(:,3),'+b')
m=size(data)
m =
40 3
[m n]=size(data)
m =
40
n =
3
M(:,1)=ones(40,1);
whos('M')
Name Size Bytes Class Attributes
M 40x1 320 double
ts = data(:,2); obs = data(:,3);
M = [ts.*M(:,1) M]
M =
3.278703000000000 1.000000000000000
0.178558000000000 1.000000000000000
4.245647000000000 1.000000000000000
4.669966000000000 1.000000000000000
3.393676000000000 1.000000000000000
3.788701000000000 1.000000000000000
3.715662000000000 1.000000000000000
1.961135000000000 1.000000000000000
3.277389000000000 1.000000000000000
0.855933000000000 1.000000000000000
3.530230000000000 1.000000000000000
0.159164000000000 1.000000000000000
1.384615000000000 1.000000000000000
0.230857000000000 1.000000000000000
0.485659000000000 1.000000000000000
4.117289000000000 1.000000000000000
3.474143000000000 1.000000000000000
1.585497000000000 1.000000000000000
4.751110000000000 1.000000000000000
0.172230000000000 1.000000000000000
2.193722000000000 1.000000000000000
1.907792000000000 1.000000000000000
3.827584000000000 1.000000000000000
3.976000000000000 1.000000000000000
0.934363000000000 1.000000000000000
2.448822000000000 1.000000000000000
2.227931000000000 1.000000000000000
3.231565000000000 1.000000000000000
3.546824000000000 1.000000000000000
3.773433000000000 1.000000000000000
1.380125000000000 1.000000000000000
3.398513000000000 1.000000000000000
3.275490000000000 1.000000000000000
0.813059000000000 1.000000000000000
0.594988000000000 1.000000000000000
2.491820000000000 1.000000000000000
4.798720000000000 1.000000000000000
1.701929000000000 1.000000000000000
2.926339000000000 1.000000000000000
1.119060000000000 1.000000000000000
M = [ts.*M(:,1) M]
M =
10.749893362209001 3.278703000000000 1.000000000000000
0.031882959364000 0.178558000000000 1.000000000000000
18.025518448608999 4.245647000000000 1.000000000000000
21.808582441155995 4.669966000000000 1.000000000000000
11.517036792976000 3.393676000000000 1.000000000000000
14.354255267401001 3.788701000000000 1.000000000000000
13.806144098243999 3.715662000000000 1.000000000000000
3.846050488225000 1.961135000000000 1.000000000000000
10.741278657320999 3.277389000000000 1.000000000000000
0.732621300489000 0.855933000000000 1.000000000000000
12.462523852900000 3.530230000000000 1.000000000000000
0.025333178896000 0.159164000000000 1.000000000000000
1.917158698225000 1.384615000000000 1.000000000000000
0.053294954449000 0.230857000000000 1.000000000000000
0.235864664281000 0.485659000000000 1.000000000000000
16.952068709521004 4.117289000000000 1.000000000000000
12.069669584449002 3.474143000000000 1.000000000000000
2.513800737009000 1.585497000000000 1.000000000000000
22.573046232099998 4.751110000000000 1.000000000000000
0.029663172900000 0.172230000000000 1.000000000000000
4.812416213284001 2.193722000000000 1.000000000000000
3.639670315264000 1.907792000000000 1.000000000000000
14.650399277056000 3.827584000000000 1.000000000000000
15.808576000000000 3.976000000000000 1.000000000000000
0.873034215769000 0.934363000000000 1.000000000000000
5.996729187683999 2.448822000000000 1.000000000000000
4.963676540760999 2.227931000000000 1.000000000000000
10.443012349224999 3.231565000000000 1.000000000000000
12.579960486976001 3.546824000000000 1.000000000000000
14.238796605488998 3.773433000000000 1.000000000000000
1.904745015625000 1.380125000000000 1.000000000000000
11.549890611168999 3.398513000000000 1.000000000000000
10.728834740100000 3.275490000000000 1.000000000000000
0.661064937481000 0.813059000000000 1.000000000000000
0.354010720144000 0.594988000000000 1.000000000000000
6.209166912400001 2.491820000000000 1.000000000000000
23.027713638400002 4.798720000000000 1.000000000000000
2.896562321041000 1.701929000000000 1.000000000000000
8.563459942921000 2.926339000000000 1.000000000000000
1.252295283600000 1.119060000000000 1.000000000000000
M = [ts.*M(:,1) M]
M =
1.0e+02 *
0.352457076163547 0.107498933622090 0.032787030000000 0.010000000000000
0.000056929574581 0.000318829593640 0.001785580000000 0.010000000000000
0.765299883247814 0.180255184486090 0.042456470000000 0.010000000000000
1.018453385083955 0.218085824411560 0.046699660000000 0.010000000000000
0.390850913554396 0.115170367929760 0.033936760000000 0.010000000000000
0.543839812858574 0.143542552674010 0.037887010000000 0.010000000000000
0.512989649923695 0.138061440982440 0.037156620000000 0.010000000000000
0.075426242242251 0.038460504882250 0.019611350000000 0.010000000000000
0.352033485174386 0.107412786573210 0.032773890000000 0.010000000000000
0.006270747475915 0.007326213004890 0.008559330000000 0.010000000000000
0.439955755812232 0.124625238529000 0.035302300000000 0.010000000000000
0.000040321300858 0.000253331788960 0.001591640000000 0.010000000000000
0.026545266909428 0.019171586982250 0.013846150000000 0.010000000000000
0.000123035132992 0.000532949544490 0.002308570000000 0.010000000000000
0.001145497969900 0.002358646642810 0.004856590000000 0.010000000000000
0.697965660249550 0.169520687095210 0.041172890000000 0.010000000000000
0.419317580991264 0.120696695844490 0.034741430000000 0.010000000000000
0.039856235271256 0.025138007370090 0.015854970000000 0.010000000000000
1.072470256837926 0.225730462321000 0.047511100000000 0.010000000000000
0.000051088882686 0.000296631729000 0.001722300000000 0.010000000000000
0.105571033202378 0.048124162132840 0.021937220000000 0.010000000000000
0.069437339100981 0.036396703152640 0.019077920000000 0.010000000000000
0.560756338664711 0.146503992770560 0.038275840000000 0.010000000000000
0.628548981760000 0.158085760000000 0.039760000000000 0.010000000000000
0.008157308689486 0.008730342157690 0.009343630000000 0.010000000000000
0.146849223628427 0.059967291876840 0.024488220000000 0.010000000000000
0.110587288391342 0.049636765407610 0.022279310000000 0.010000000000000
0.337472732023233 0.104430123492250 0.032315650000000 0.010000000000000
0.446189057742582 0.125799604869760 0.035468240000000 0.010000000000000
0.537291449914402 0.142387966054890 0.037734330000000 0.010000000000000
0.026287862146895 0.019047450156250 0.013801250000000 0.010000000000000
0.392524533906358 0.115498906111690 0.033985130000000 0.010000000000000
0.351421909028501 0.107288347401000 0.032754900000000 0.010000000000000
0.005374847970034 0.006610649374810 0.008130590000000 0.010000000000000
0.002106321303570 0.003540107201440 0.005949880000000 0.010000000000000
0.154721262956566 0.062091669124000 0.024918200000000 0.010000000000000
1.105035499908629 0.230277136384000 0.047987200000000 0.010000000000000
0.049297434144870 0.028965623210410 0.017019290000000 0.010000000000000
0.250595868059075 0.085634599429210 0.029263390000000 0.010000000000000
0.014013935600654 0.012522952836000 0.011190600000000 0.010000000000000
M = [ts.*M(:,1) M];
whos('M')
Name Size Bytes Class Attributes
M 40x5 1600 double
coeff = zeros(5,4)
coeff =
0 0 0 0
0 0 0 0
0 0 0 0
0 0 0 0
0 0 0 0
iter=1; coeff(1:iter+1, iter) = M(:, end-iter:end) \ obs
coeff =
0.647833370275578 0 0 0
11.262033230552547 0 0 0
0 0 0 0
0 0 0 0
0 0 0 0
iter=2; coeff(1:iter+1, iter) = M(:, end-iter:end) \ obs
coeff =
0.647833370275578 2.443486049535086 0 0
11.262033230552547 -10.865171168884011 0 0
0 19.859650399882096 0 0
0 0 0 0
0 0 0 0
iter=3; coeff(1:iter+1, iter) = M(:, end-iter:end) \ obs
coeff =
0.647833370275578 2.443486049535086 1.017975603858307 0
11.262033230552547 -10.865171168884011 -5.059699860040298 0
0 19.859650399882096 3.968517438474690 0
0 0 14.083280969961683 0
0 0 0 0
iter=4; coeff(1:iter+1, iter) = M(:, end-iter:end) \ obs
coeff =
0.647833370275578 2.443486049535086 1.017975603858307 -0.113702764591520
11.262033230552547 -10.865171168884011 -5.059699860040298 2.146601817601733
0 19.859650399882096 3.968517438474690 -8.646574125002362
0 0 14.083280969961683 7.850705179344446
0 0 0 13.179039199295232
min(ts)
ans =
0.159164000000000
max(ts)
ans =
4.798720000000000
p1 = @(x) coeff(1,1) * x + coeff(1,2)
p1 =
@(x)coeff(1,1)*x+coeff(1,2)
p1 = @fun\(x) coeff(1,1) * x + coeff(1,2)
p1 = @fun\(x) coeff(1,1) * x + coeff(1,2)
|
{Error: Unexpected MATLAB expression.
}
p1 = @fun(x) coeff(1,1) * x + coeff(1,2)
p1 = @fun(x) coeff(1,1) * x + coeff(1,2)
|
{Error: Unbalanced or unexpected parenthesis or bracket.
}
p1 = fun(x) coeff(1,1) * x + coeff(1,2)
p1 = fun(x) coeff(1,1) * x + coeff(1,2)
|
{Error: Unexpected MATLAB expression.
}
p1 = @(x) (coeff(1,1) * x + coeff(1,2))
p1 =
@(x)(coeff(1,1)*x+coeff(1,2))
p1(3_
p1(3_
|
{Error: The input character is not valid in MATLAB statements or expressions.
}
p1(3)
ans =
4.386986160361820
p1 = @(x) coeff(1,1) * x + coeff(1,2)
p1 =
@(x)coeff(1,1)*x+coeff(1,2)
p1(0)
ans =
2.443486049535086
coeff'
ans =
0.647833370275578 11.262033230552547 0 0 0
2.443486049535086 -10.865171168884011 19.859650399882096 0 0
1.017975603858307 -5.059699860040298 3.968517438474690 14.083280969961683 0
-0.113702764591520 2.146601817601733 -8.646574125002362 7.850705179344446 13.179039199295232
coeff
coeff =
0.647833370275578 2.443486049535086 1.017975603858307 -0.113702764591520
11.262033230552547 -10.865171168884011 -5.059699860040298 2.146601817601733
0 19.859650399882096 3.968517438474690 -8.646574125002362
0 0 14.083280969961683 7.850705179344446
0 0 0 13.179039199295232
coeff
coeff =
0.647833370275578 2.443486049535086 1.017975603858307 -0.113702764591520
11.262033230552547 -10.865171168884011 -5.059699860040298 2.146601817601733
0 19.859650399882096 3.968517438474690 -8.646574125002362
0 0 14.083280969961683 7.850705179344446
0 0 0 13.179039199295232
p1 = @(x) coeff(1,1) * x + coeff(2,1)
p1 =
@(x)coeff(1,1)*x+coeff(2,1)
p2 = @(x) coeff(1,2) * x.^2 + coeff(2,2) * x + coeff(3,2)
p2 =
@(x)coeff(1,2)*x.^2+coeff(2,2)*x+coeff(3,2)
p3 = @(x) coeff(1,3) * x.^3 + coeff(2,3) * x.^2 + coeff(3,3) * x + coeff(3,4)
p3 =
@(x)coeff(1,3)*x.^3+coeff(2,3)*x.^2+coeff(3,3)*x+coeff(3,4)
p3 = @(x) coeff(1,3) * x.^3 + coeff(2,3) * x.^2 + coeff(3,3) * x + coeff(4,3)
p3 =
@(x)coeff(1,3)*x.^3+coeff(2,3)*x.^2+coeff(3,3)*x+coeff(4,3)
p4 = @(x) coeff(1,4) * x.^4 + coeff(2,4) * x.^3 + coeff(3,4) * x.^2 + coeff(4,4) * x + coeff(5,4)
p4 =
@(x)coeff(1,4)*x.^4+coeff(2,4)*x.^3+coeff(3,4)*x.^2+coeff(4,4)*x+coeff(5,4)
p4(3)
ans =
7.660312755641019
help ezplot
ezplot Easy to use function plotter
ezplot(FUN) plots the function FUN(X) over the default domain
-2*PI < X < 2*PI, where FUN(X) is an explicitly defined function of X.
ezplot(FUN2) plots the implicitly defined function FUN2(X,Y) = 0 over
the default domain -2*PI < X < 2*PI and -2*PI < Y < 2*PI.
ezplot(FUN,[A,B]) plots FUN(X) over A < X < B.
ezplot(FUN2,[A,B]) plots FUN2(X,Y) = 0 over A < X < B and A < Y < B.
ezplot(FUN2,[XMIN,XMAX,YMIN,YMAX]) plots FUN2(X,Y) = 0 over
XMIN < X < XMAX and YMIN < Y < YMAX.
ezplot(FUNX,FUNY) plots the parametrically defined planar curve FUNX(T)
and FUNY(T) over the default domain 0 < T < 2*PI.
ezplot(FUNX,FUNY,[TMIN,TMAX]) plots FUNX(T) and FUNY(T) over
TMIN < T < TMAX.
ezplot(FUN,[A,B],FIG), ezplot(FUN2,[XMIN,XMAX,YMIN,YMAX],FIG), or
ezplot(FUNX,FUNY,[TMIN,TMAX],FIG) plots the function over the
specified domain in the figure window FIG.
ezplot(AX,...) plots into AX instead of GCA or FIG.
H = ezplot(...) returns handles to the plotted objects in H.
Examples:
The easiest way to express a function is via a string:
ezplot('x^2 - 2*x + 1')
One programming technique is to vectorize the string expression using
the array operators .* (TIMES), ./ (RDIVIDE), .\ (LDIVIDE), .^ (POWER).
This makes the algorithm more efficient since it can perform multiple
function evaluations at once.
ezplot('x.*y + x.^2 - y.^2 - 1')
You may also use a function handle to an existing function. Function
handles are more powerful and efficient than string expressions.
ezplot(@humps)
ezplot(@cos,@sin)
ezplot plots the variables in string expressions alphabetically.
subplot(1,2,1), ezplot('1./z - log(z) + log(-1+z) + t - 1')
To avoid this ambiguity, specify the order with an anonymous function:
subplot(1,2,2), ezplot(@(z,t)1./z - log(z) + log(-1+z) + t - 1)
If your function has additional parameters, for example k in myfun:
%-----------------------%
function z = myfun(x,y,k)
z = x.^k - y.^k - 1;
%-----------------------%
then you may use an anonymous function to specify that parameter:
ezplot(@(x,y)myfun(x,y,2))
See also ezcontour, ezcontourf, ezmesh, ezmeshc, ezplot3, ezpolar,
ezsurf, ezsurfc, plot, vectorize, function_handle.
Overloaded methods:
sym/ezplot
Reference page in Help browser
doc ezplot
pts = linspace(0,5,100);
p1(pts);
hold on;
plot(pts,p1(pts))
plot(pts,p2(pts))
plot(pts,p3(pts))
plot(pts,p4(pts))
p3(2.5)
ans =
8.287319251182591
close all
ls
D43.jpg diary-1.txt fun.m my_lu.m my_pi_up.m
data.txt diary-2.txt hw1.m my_lu2.m p1.m
diary diary-3.txt my_fft.m my_pi_down.m
img = imread('D43.jpg');
imshow(img)
[Warning: Image is too big to fit on screen; displaying at 25%]
[> In imuitools/private/initSize at 71
In imshow at 282]
whos('img')
Name Size Bytes Class Attributes
img 1365x2048x3 8386560 uint8
my_filter
whos('newimg')
Name Size Bytes Class Attributes
newimg 1365x2048x3 67092480 double
my_filter
my_filter
{Undefined function 'imgshow' for input arguments of type 'double'.
Error in my_filter (line 15)
imgshow(newimg)
}
my_filter
[Warning: Image is too big to fit on screen; displaying at 25%]
[> In imuitools/private/initSize at 71
In imshow at 282
In my_filter at 15]
my_filter
[Warning: Image is too big to fit on screen; displaying at 25%]
[> In imuitools/private/initSize at 71
In imshow at 282
In my_filter at 15]
my_filter
[Warning: Image is too big to fit on screen; displaying at 25%]
[> In imuitools/private/initSize at 71
In imshow at 282
In my_filter at 15]
my_filter
[Warning: Image is too big to fit on screen; displaying at 25%]
[> In imuitools/private/initSize at 71
In imshow at 282
In my_filter at 19]
my_filter
[Warning: Image is too big to fit on screen; displaying at 25%]
[> In imuitools/private/initSize at 71
In imshow at 282
In my_filter at 8]
my_filter
[Warning: Image is too big to fit on screen; displaying at 25%]
[> In imuitools/private/initSize at 71
In imshow at 282
In my_filter at 8]
my_filter
[Warning: Displaying real part of complex input.]
[> In imuitools/private/imageDisplayValidateParams>validateCData at 141
In imuitools/private/imageDisplayValidateParams at 27
In imuitools/private/imageDisplayParseInputs at 78
In imshow at 219
In my_filter at 10]
[Warning: Image is too big to fit on screen; displaying at 25%]
[> In imuitools/private/initSize at 71
In imshow at 282
In my_filter at 10]
my_filter
[Warning: Displaying real part of complex input.]
[> In imuitools/private/imageDisplayValidateParams>validateCData at 141
In imuitools/private/imageDisplayValidateParams at 27
In imuitools/private/imageDisplayParseInputs at 78
In imshow at 219
In my_filter at 10]
[Warning: Image is too big to fit on screen; displaying at 25%]
[> In imuitools/private/initSize at 71
In imshow at 282
In my_filter at 10]
whos('freq1')
Name Size Bytes Class Attributes
freq1 1365x2048 44728320 double complex
my_filter
my_filter
{Undefined function 'iff2' for input arguments of type 'double'.
Error in my_filter (line 13)
newimg(:,:,1)=iff2(ifftshift(freq1));
}
my_filter
[Warning: Image is too big to fit on screen; displaying at 25%]
[> In imuitools/private/initSize at 71
In imshow at 282
In my_filter at 26]
my_filter
[Warning: Image is too big to fit on screen; displaying at 25%]
[> In imuitools/private/initSize at 71
In imshow at 282
In my_filter at 4]
[Warning: Image is too big to fit on screen; displaying at 25%]
[> In imuitools/private/initSize at 71 In imuitools/private/initSize at 71
In imshow at 282
In my_filter at 5]
[Warning: Image is too big to fit on screen; displaying at 25%]
[> In imuitools/private/initSize at 71
In imshow at 282
In my_filter at 29]
[Warning: Image is too big to fit on screen; displaying at 25%]
[> In imuitools/private/initSize at 71
In imshow at 282
In my_filter at 35]
my_filter
[Warning: Image is too big to fit on screen; displaying at 25%]
[> In imuitools/private/initSize at 71
In imshow at 282
In my_filter at 5]
[Warning: Image is too big to fit on screen; displaying at 25%]
[> In imuitools/private/initSize at 71
In imshow at 282
In my_filter at 29]
[Warning: Image is too big to fit on screen; displaying at 25%]
[> In imuitools/private/initSize at 71
In imshow at 282
In my_filter at 35]
my_filter
[Warning: Image is too big to fit on screen; displaying at 25%]
[> In imuitools/private/initSize at 71
In imshow at 282
In my_filter at 5]
[Warning: Image is too big to fit on screen; displaying at 25%]
[> In imuitools/private/initSize at 71
In imshow at 282
In my_filter at 30]
my_filter
[Warning: Image is too big to fit on screen; displaying at 25%]
[> In imuitools/private/initSize at 71
In imshow at 282
In my_filter at 5]
[Warning: Image is too big to fit on screen; displaying at 25%]
[> In imuitools/private/initSize at 71
In imshow at 282
In my_filter at 30]
[Warning: Image is too big to fit on screen; displaying at 25%]
[> In imuitools/private/initSize at 71
In imshow at 282
In my_filter at 36]
norm(rand(2,3,4))
{Error using norm
Input must be 2-D.
}
my_filter
[Warning: Image is too big to fit on screen; displaying at 25%]
[> In imuitools/private/initSize at 71
In imshow at 282
In my_filter at 5]
[Warning: Image is too big to fit on screen; displaying at 25%]
[> In imuitools/private/initSize at 71
In imshow at 282
In my_filter at 30]
my_filter
[Warning: Image is too big to fit on screen; displaying at 25%]
[> In imuitools/private/initSize at 71
In imshow at 282
In my_filter at 5]
[Warning: Image is too big to fit on screen; displaying at 25%]
[> In imuitools/private/initSize at 71
In imshow at 282
In my_filter at 30]
my_filter
[Warning: Image is too big to fit on screen; displaying at 25%]
[> In imuitools/private/initSize at 71
In imshow at 282
In my_filter at 5]
[Warning: Image is too big to fit on screen; displaying at 25%]
[> In imuitools/private/initSize at 71
In imshow at 282
In my_filter at 30]
+ - \ / *
+ - \ / *
|
{Error: Unexpected MATLAB operator.
}
sparse
{Error using sparse
Not enough input arguments.
}
help norm
norm Matrix or vector norm.
norm(X,2) returns the 2-norm of X.
norm(X) is the same as norm(X,2).
norm(X,1) returns the 1-norm of X.
norm(X,Inf) returns the infinity norm of X.
norm(X,'fro') returns the Frobenius norm of X.
In addition, for vectors...
norm(V,P) returns the p-norm of V defined as SUM(ABS(V).^P)^(1/P).
norm(V,Inf) returns the largest element of ABS(V).
norm(V,-Inf) returns the smallest element of ABS(V).
By convention, NaN is returned if X or V contains NaNs.
See also cond, rcond, condest, normest, hypot.
Overloaded methods:
DynamicSystem/norm
codistributed/norm
gpuArray/norm
mfilt.norm
adaptfilt.norm
dfilt.norm
sym/norm
Reference page in Help browser
doc norm
help lu
lu lu factorization.
[L,U] = lu(A) stores an upper triangular matrix in U and a
"psychologically lower triangular matrix" (i.e. a product of lower
triangular and permutation matrices) in L, so that A = L*U. A can be
rectangular.
[L,U,P] = lu(A) returns unit lower triangular matrix L, upper
triangular matrix U, and permutation matrix P so that P*A = L*U.
[L,U,p] = lu(A,'vector') returns the permutation information as a
vector instead of a matrix. That is, p is a row vector such that
A(p,:) = L*U. Similarly, [L,U,P] = lu(A,'matrix') returns a
permutation matrix P. This is the default behavior.
Y = lu(A) returns the output from LAPACK'S DGETRF or ZGETRF routine if
A is full. If A is sparse, Y contains the strict lower triangle of L
embedded in the same matrix as the upper triangle of U. In both full
and sparse cases, the permutation information is lost.
[L,U,P,Q] = lu(A) returns unit lower triangular matrix L, upper
triangular matrix U, a permutation matrix P and a column reordering
matrix Q so that P*A*Q = L*U for sparse non-empty A. This uses UMFPACK
and is significantly more time and memory efficient than the other
syntaxes, even when used with COLAMD.
[L,U,p,q] = lu(A,'vector') returns two row vectors p and q so that
A(p,q) = L*U. Using 'matrix' in place of 'vector' returns permutation
matrices.
[L,U,P,Q,R] = lu(A) returns unit lower triangular matrix L, upper
triangular matrix U, permutation matrices P and Q, and a diagonal
scaling matrix R so that P*(R\A)*Q = L*U for sparse non-empty A.
This uses UMFPACK as well. Typically, but not always, the row-scaling
leads to a sparser and more stable factorization. Note that this
factorization is the same as that used by sparse MLDIVIDE when
UMFPACK is used.
[L,U,p,q,R] = lu(A,'vector') returns the permutation information in two
row vectors p and q such that R(:,p)\A(:,q) = L*U. Using 'matrix'
in place of 'vector' returns permutation matrices.
[L,U,P] = lu(A,THRESH) controls pivoting in sparse matrices, where
THRESH is a pivot threshold in [0,1]. Pivoting occurs when the
diagonal entry in a column has magnitude less than THRESH times the
magnitude of any sub-diagonal entry in that column. THRESH = 0 forces
diagonal pivoting. THRESH = 1 is the default.
[L,U,P,Q,R] = lu(A,THRESH) controls pivoting in UMFPACK. THRESH is a
one or two element vector which defaults to [0.1 0.001]. If UMFPACK
selects its unsymmetric pivoting strategy, THRESH(2) is not used. It
uses its symmetric pivoting strategy if A is square with a mostly
symmetric nonzero structure and a mostly nonzero diagonal. For its
unsymmetric strategy, the sparsest row i which satisfies the criterion
A(i,j) >= THRESH(1) * max(abs(A(j:m,j))) is selected. A value of 1.0
results in conventional partial pivoting. Entries in L have absolute
value of 1/THRESH(1) or less. For its symmetric strategy, the diagonal
is selected using the same test but with THRESH(2) instead. If the
diagonal entry fails this test, a pivot entry below the diagonal is
selected, using THRESH(1). In this case, L has entries with absolute
value 1/min(THRESH) or less. Smaller values of THRESH(1) and THRESH(2)
tend to lead to sparser lu factors, but the solution can become
inaccurate. Larger values can lead to a more accurate solution (but
not always), and usually an increase in the total work and memory
usage.
[L,U,p] = lu(A,THRESH,'vector') and [L,U,p,q,R] = lu(A,THRESH,'vector')
are also valid for sparse matrices and return permutation vectors.
Using 'matrix' in place of 'vector' returns permutation matrices.
See also chol, ilu, qr.
Overloaded methods:
gf/lu
codistributed/lu
gpuArray/lu
sym/lu
Reference page in Help browser
doc lu
P
{Undefined function or variable 'P'.
}
help chol
chol Cholesky factorization.
chol(A) uses only the diagonal and upper triangle of A.
The lower triangle is assumed to be the (complex conjugate)
transpose of the upper triangle. If A is positive definite, then
R = chol(A) produces an upper triangular R so that R'*R = A.
If A is not positive definite, an error message is printed.
L = chol(A,'lower') uses only the diagonal and the lower triangle
of A to produce a lower triangular L so that L*L' = A. If
A is not positive definite, an error message is printed. When
A is sparse, this syntax of chol is typically faster.
[R,p] = chol(A), with two output arguments, never produces an
error message. If A is positive definite, then p is 0 and R
is the same as above. But if A is not positive definite, then
p is a positive integer.
When A is full, R is an upper triangular matrix of order q = p-1
so that R'*R = A(1:q,1:q).
When A is sparse, R is an upper triangular matrix of size q-by-n
so that the L-shaped region of the first q rows and first q
columns of R'*R agree with those of A.
[L,p] = chol(A,'lower'), functions as described above, only a lower
triangular matrix L is produced. That is, when A is full, L is a
lower triangular matrix of order q = p-1 so that L*L' = A(1:q,1:q).
When A is sparse, L is a lower triangular matrix of size n-by-q
so that the L-shaped region of the first q rows and first q columns
of L*L' agree with those of A.
[R,p,S] = chol(A), when A is sparse, returns a permutation matrix
S which is a preordering of A obtained by AMD. When p = 0, R is an
upper triangular matrix such that R'*R = S'*A*S. When p is not zero,
R is an upper triangular matrix of size q-by-n so that the L-shaped
region of the first q rows and first q columns of R'*R agree with
those of S'*A*S. The factor of S'*A*S tends to be sparser than the
factor of A.
[R,p,s] = chol(A,'vector') returns the permutation information as
a vector s such that A(s,s) = R'*R, when p = 0. The flag 'matrix'
may be used in place of 'vector' to obtain the default behavior.
[L,p,s] = chol(A,'lower','vector') uses only the diagonal and the
lower triangle of A and returns a lower triangular matrix L and
a permutation vector s such that A(s,s) = L*L', when p = 0.
As above, 'matrix' may be used in place of 'vector' to obtain a
permutation matrix.
For sparse A, CHOLMOD is used to compute the Cholesky factor.
See also cholupdate, ichol, ldl, lu.
Overloaded methods:
codistributed/chol
gpuArray/chol
sym/chol
Reference page in Help browser
doc chol
help qr
qr Orthogonal-triangular decomposition.
[Q,R] = qr(A), where A is m-by-n, produces an m-by-n upper triangular
matrix R and an m-by-m unitary matrix Q so that A = Q*R.
[Q,R] = qr(A,0) produces the "economy size" decomposition.
If m>n, only the first n columns of Q and the first n rows of R are
computed. If m<=n, this is the same as [Q,R] = qr(A).
If A is full:
[Q,R,E] = qr(A) produces unitary Q, upper triangular R and a
permutation matrix E so that A*E = Q*R. The column permutation E is
chosen so that ABS(DIAG(R)) is decreasing.
[Q,R,e] = qr(A,'vector') returns the permutation information as a
vector instead of a matrix. That is, e is a row vector such that
A(:,e) = Q*R. Similarly, [Q,R,E] = qr(A,'matrix') returns a permutation
matrix E. This is the default behavior.
[Q,R,E] = qr(A,0) produces an "economy size" decomposition in which E
is a permutation vector, so that A(:,E) = Q*R.
X = qr(A) and X = qr(A,0) return the output of LAPACK's *GEQRF routine.
TRIU(X) is the upper triangular factor R.
If A is sparse:
R = qr(A) computes a "Q-less qr decomposition" and returns the upper
triangular factor R. Note that R = CHOL(A'*A). Since Q is often nearly
full, this is preferred to [Q,R] = qr(A).
R = qr(A,0) produces "economy size" R. If m>n, R has only n rows. If
m<=n, this is the same as R = qr(A).
[Q,R,E] = qr(A) produces unitary Q, upper triangular R and a
permutation matrix E so that A*E = Q*R. The column permutation E is
chosen to reduce fill-in in R.
[Q,R,e] = qr(A,'vector') returns the permutation information as a
vector instead of a matrix. That is, e is a row vector such that
A(:,e) = Q*R. Similarly, [Q,R,E] = qr(A,'matrix') returns a permutation
matrix E. This is the default behavior.
[Q,R,E] = qr(A,0) produces an "economy size" decomposition in which E
is a permutation vector, so that A(:,E) = Q*R.
[C,R] = qr(A,B), where B has as many rows as A, returns C = Q'*B.
The least-squares solution to A*X = B is X = R\C.
[C,R,E] = qr(A,B), also returns a fill-reducing ordering.
The least-squares solution to A*X = B is X = E*(R\C).
[C,R,e] = qr(A,B,'vector') returns the permutation information as a
vector instead of a matrix. That is, the least-squares solution to
A*X = B is X(e,:) = R\C. Similarly, [C,R,E] = qr(A,B,'matrix') returns
a permutation matrix E. This is the default behavior.
[C,R] = qr(A,B,0) produces "economy size" results. If m>n, C and R have
only n rows. If m<=n, this is the same as [C,R] = qr(A,B).
[C,R,E] = qr(A,B,0) additionally produces a fill-reducing permutation
vector E. In this case, the least-squares solution to A*X = B is
X(E,:) = R\C.
Example: The least squares approximate solution to A*x = b can be found
with the Q-less qr decomposition and one step of iterative refinement:
if issparse(A), R = qr(A); else R = triu(qr(A)); end
x = R\(R'\(A'*b));
r = b - A*x;
e = R\(R'\(A'*r));
x = x + e;
See also lu, null, orth, qrdelete, qrinsert, qrupdate.
Overloaded methods:
codistributed/qr
gpuArray/qr
sym/qr
Reference page in Help browser
doc qr
help eig
eig Eigenvalues and eigenvectors.
E = eig(A) produces a column vector E containing the eigenvalues of
a square matrix A.
[V,D] = eig(A) produces a diagonal matrix D of eigenvalues and
a full matrix V whose columns are the corresponding eigenvectors
so that A*V = V*D.
[V,D,W] = eig(A) also produces a full matrix W whose columns are the
corresponding left eigenvectors so that W'*A = D*W'.
[...] = eig(A,'nobalance') performs the computation with balancing
disabled, which sometimes gives more accurate results for certain
problems with unusual scaling. If A is symmetric, eig(A,'nobalance')
is ignored since A is already balanced.
[...] = eig(A,'balance') is the same as eig(A).
E = eig(A,B) produces a column vector E containing the generalized
eigenvalues of square matrices A and B.
[V,D] = eig(A,B) produces a diagonal matrix D of generalized
eigenvalues and a full matrix V whose columns are the corresponding
eigenvectors so that A*V = B*V*D.
[V,D,W] = eig(A,B) also produces a full matrix W whose columns are the
corresponding left eigenvectors so that W'*A = D*W'*B.
[...] = eig(A,B,'chol') is the same as eig(A,B) for symmetric A and
symmetric positive definite B. It computes the generalized eigenvalues
of A and B using the Cholesky factorization of B.
[...] = eig(A,B,'qz') ignores the symmetry of A and B and uses the QZ
algorithm. In general, the two algorithms return the same result,
however using the QZ algorithm may be more stable for certain problems.
The flag is ignored when A or B are not symmetric.
[...] = eig(...,'vector') returns eigenvalues in a column vector
instead of a diagonal matrix.
[...] = eig(...,'matrix') returns eigenvalues in a diagonal matrix
instead of a column vector.
See also condeig, eigs, ordeig.
Overloaded methods:
codistributed/eig
gpuArray/eig
sym/eig
Reference page in Help browser
doc eig
help svd
svd Singular value decomposition.
[U,S,V] = svd(X) produces a diagonal matrix S, of the same
dimension as X and with nonnegative diagonal elements in
decreasing order, and unitary matrices U and V so that
X = U*S*V'.
S = svd(X) returns a vector containing the singular values.
[U,S,V] = svd(X,0) produces the "economy size"
decomposition. If X is m-by-n with m > n, then only the
first n columns of U are computed and S is n-by-n.
For m <= n, svd(X,0) is equivalent to svd(X).
[U,S,V] = svd(X,'econ') also produces the "economy size"
decomposition. If X is m-by-n with m >= n, then it is
equivalent to svd(X,0). For m < n, only the first m columns
of V are computed and S is m-by-m.
See also svds, gsvd.
Overloaded methods:
codistributed/svd
gpuArray/svd
frd/svd
sym/svd
Reference page in Help browser
doc svd
diary off