Matlab學(xué)習(xí)過程中的一些小問題
1.Overload your functions by having variable number of input and output argumernt.Not only can we overload functions also operators.
我們可以通過不同的輸入輸出來重載函數(shù),當(dāng)然我們還可以重載運(yùn)算符。一般來說,運(yùn)算符重載只在OOP中使用到。(see varargin,varargout,nargin,nargout)
varargin :
Allows any number of arguments to a function. The variable varargin is a cell array containing the optional arguments to the function. varargin must be declared as the last input argument and collects all the inputs from that point onwards. In the declaration, varargin must be lowercase (i.e., varargin).
允許函數(shù)輸入任意數(shù)量的參數(shù),varargin實(shí)際上是一個(gè)cell,包含著函數(shù)的可選參數(shù)。varargin參數(shù)必須在參數(shù)列表的最右面,在聲明時(shí),varargin必須小寫!
For example, the function,
例如,函數(shù):
function myplot(x,varargin)
plot(x,varargin{:})
collects all the inputs starting with the second input into the variable "varargin". MYPLOT uses the comma-separated list syntax varargin{:} to pass the optional parameters to plot.
這個(gè)函數(shù)把從第二個(gè)輸入的參數(shù)全部收集進(jìn)變量"varargin"
The call,
>> myplot(sin(0:.1:1),'color',[.5 .7 .3],'linestyle',':')
results in varargin being a 1-by-4 cell array containing the
values 'color', [.5 .7 .3], 'linestyle', and ':'.
varargout:ariable length output argument list.
Allows any number of output arguments from a function. The variable varargout is a cell array containing the optional output arguments from the function. varargout must be declared as the last output argument and must contain all the outputs after that point onwards. In the declaration, varargout must be lowercase (i.e., varargout).
允許函數(shù)任意數(shù)量的輸出,varargout和varargin的數(shù)據(jù)類型一樣,是一個(gè)cell,包含了函數(shù)的可選的任意輸出,同樣varargout也必須是在輸出的左后一個(gè)參數(shù),在聲明時(shí),varargout必須小寫!
varargout is not initialized when the function is invoked. You must create it before your function returns. Use NARGOUT to determine the number of outputs to produce.
varargout直至函數(shù)被調(diào)用時(shí)才被初始化,你必須在你的函數(shù)返回前創(chuàng)造他,使用NARGOUT來決定要產(chǎn)生的輸出(總)的數(shù)目。
For example, the function,
例:
function [s,varargout] = mysize(x)
nout = max(nargout,1)-1;
s = size(x);
for i=1:nout, varargout(i) = {s(i)};
%提前創(chuàng)造varargin(remember varargout is a cell)
end
returns the size vector and optionally individual sizes. So,
>>[s,rows,cols] = mysize(rand(4,5));
>> s = [4 5], rows = 4, cols = 5.
2.Visualizing matrices 的幾個(gè)函數(shù)
a.colormap b.surf c.contour d.colorbar e.imagesc
(all thiese are built-inn functions)
3.計(jì)算程序運(yùn)行的時(shí)間;
tic ....toc1...toc2...此外還有for more complicated situation >>profile on;>>profile viewer
4.MATLAB 格式化輸出
fprintf :write formatted data to text file.將格式化數(shù)據(jù)寫入文本文檔。
fprintf(FID, FORMAT, A, ...) applies the FORMAT to all elements of array A and any additional array arguments in column order, and writes the data to a text file. FID is an integer file identifier. Obtain FID from FOPEN, or set it to 1 (for standard output, the screen) or 2(standard error). fprintf uses the encoding scheme specified in the call to FOPEN.
將FORMAT應(yīng)用到A的所有元素中和其他附加的以列為順序的參數(shù),然后將這些數(shù)據(jù)寫到一個(gè)文本文檔,F(xiàn)ID是一個(gè)文件的辨識(shí)符,從fopen中國獲得fid,或者將其設(shè)置為1(標(biāo)準(zhǔn)輸出到屏幕上)或者2 (標(biāo)準(zhǔn)錯(cuò)誤)。
fprintf(FORMAT, A, ...) formats data and displays the results on the screen.
格式化數(shù)據(jù)并將其輸出到屏幕上。
COUNT = fprintf(...) returns the number of bytes that fprintf writes.
計(jì)算fprintf寫了多少字節(jié)。
FORMAT is a string that describes the format of the output fields, and can include combinations of the following:
FORMAT是一個(gè)字符串,它描述輸出的格式:
* Conversion specifications, which include a % character, a conversion character (such as d, i, o, u, x, f, e, g, c, or s), and optional flags, width, and precision fields. For more details, type "doc fprintf" at the command prompt.
* Literal text to print.
* Escape characters, including:
\b Backspace '' Single quotation mark
\f Form feed %% Percent character
\n New line \\ Backslash
\r Carriage return \xN Hexadecimal number N
\t Horizontal tab \N Octal number N
For most cases, \n is sufficient for a single line break.
However, if you are creating a file for use with Microsoft
Notepad, specify a combination of \r\n to move to a new line.
Notes:
If you apply an integer or string conversion to a numeric value that contains a fraction, MATLAB overrides the specified conversion, and uses %e.
Numeric conversions print only the real component of complex numbers.Example: Create a text file called exp.txt containing a short table of the exponential function.
x = 0:.1:1;
y = [x; exp(x)];
fid = fopen('exp.txt','w');
fprintf(fid,'%6.2f %12.8f\n',y);
fclose(fid);
Examine the contents of exp.txt:
type exp.txt
MATLAB returns:
0.00 1.00000000
0.10 1.10517092
...
1.00 2.71828183
sprintf:僅格式化并不輸出,聲明同fprintf。
5.多樣化的MATLAB(Miscellaneous MATLAB)
幾個(gè)關(guān)鍵字:deal,eval, repmat
一個(gè)表達(dá)式:regular expression
6.使用cell和struct的幾個(gè)注意事項(xiàng)



注意cell的索引方式。cell使用前一般提前聲明和prelocation.
另;struct一般不提前聲明??聪铝写a:

a = struct;
a(1).name = 'Leee';
a(1).age = 25;
a(2).name = 'bbb';
a(2).age = 32;
%或者聲明+初始化
b = struct('name',{'lee','faker'},'age',{25,34});
初始化+聲明同步進(jìn)行時(shí),key-valu的value應(yīng)該用{ },但如果西先聲明后賦值的化依舊使用{ } ,就會(huì)給出現(xiàn)這種情況:
a = struct;
a.name = {'Leee','bbbb'};
a.age ={25,32};

我們還可以通過使用deal對struct進(jìn)行批量化的賦值:
a =struct;
cc = {'x',1,2,3,4};
for i=1:5
a(i).name = deal('x');
a(i).age = deal(10);
end
以下代碼會(huì)創(chuàng)建一個(gè)Vector
ageVec = [a.age];
則輸出如下:
7.OOP
談到OOP,必須講到的封裝,繼承,多態(tài)。
先給出一個(gè)定義matlab中類的典型代碼:
%class contact
classdef contact
properties%默認(rèn)屬性為public
name
phonenumber
end
properties(SetAccess = private)%私有屬性,還可以設(shè)置讀取的屬性GetAccess
gender
end
methods
function obj = contact(m_name,m_phonenumber,m_gender)
obj.name=m_name;
obj.phonenumber = num2str(m_phonenumber);
obj.gender = m_gender;
end
function disp(obj)
fprintf('%s is %s, phonenumber is %s\n',obj.name,...
obj.gender,obj.phonenumber)
end
end
end
第一個(gè)方法時(shí)類的構(gòu)造函數(shù),第二個(gè)方法我們重載了函數(shù)disp.
8.Linked List

浙公網(wǎng)安備 33010602011771號