Matlab学习过程中的一些小问题
1.Overload your functions by having variable number of input and output argumernt.Not only can we overload functions also operators.
我们可以通过不同的输入输出来重载函数,当然我们还可以重载运算符。一般来说,运算符重载只在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).
允许函数输入任意数量的参数,varargin实际上是一个cell,包含着函数的可选参数。varargin参数必须在参数列表的最右面,在声明时,varargin必须小写!
For example, the function,
例如,函数:
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.
这个函数把从第二个输入的参数全部收集进变量"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).
允许函数任意数量的输出,varargout和varargin的数据类型一样,是一个cell,包含了函数的可选的任意输出,同样varargout也必须是在输出的左后一个参数,在声明时,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直至函数被调用时才被初始化,你必须在你的函数返回前创造他,使用NARGOUT来决定要产生的输出(总)的数目。
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)};
%提前创造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 的几个函数
a.colormap b.surf c.contour d.colorbar e.imagesc
(all thiese are built-inn functions)
3.计算程序运行的时间;
tic ....toc1...toc2...此外还有for more complicated situation >>profile on;>>profile viewer
4.MATLAB 格式化输出
fprintf :write formatted data to text file.将格式化数据写入文本文档。
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应用到A的所有元素中和其他附加的以列为顺序的参数,然后将这些数据写到一个文本文档,FID是一个文件的辨识符,从fopen中国获得fid,或者将其设置为1(标准输出到屏幕上)或者2 (标准错误)。
fprintf(FORMAT, A, ...) formats data and displays the results on the screen.
格式化数据并将其输出到屏幕上。
COUNT = fprintf(...) returns the number of bytes that fprintf writes.
计算fprintf写了多少字节。
FORMAT is a string that describes the format of the output fields, and can include combinations of the following:
FORMAT是一个字符串,它描述输出的格式:
* 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)
几个关键字:deal,eval, repmat
一个表达式:regular expression
6.使用cell和struct的几个注意事项



注意cell的索引方式。cell使用前一般提前声明和prelocation.
另;struct一般不提前声明。看下列代码:

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});
初始化+声明同步进行时,key-valu的value应该用{ },但如果西先声明后赋值的化依旧使用{ } ,就会给出现这种情况:
a = struct;
a.name = {'Leee','bbbb'};
a.age ={25,32};

我们还可以通过使用deal对struct进行批量化的赋值:
a =struct;
cc = {'x',1,2,3,4};
for i=1:5
a(i).name = deal('x');
a(i).age = deal(10);
end
以下代码会创建一个Vector
ageVec = [a.age];
则输出如下:
7.OOP
谈到OOP,必须讲到的封装,继承,多态。
先给出一个定义matlab中类的典型代码:
%class contact
classdef contact
properties%默认属性为public
name
phonenumber
end
properties(SetAccess = private)%私有属性,还可以设置读取的属性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
第一个方法时类的构造函数,第二个方法我们重载了函数disp.
8.Linked List
Matlab学习过程中的一些小问题的更多相关文章
- Vue实例学习过程中碰到的小问题
在使用插值表达式{{ }}取data中list数组中的值时把整个表达式当做文本显示了,原因不明,但是使用v-text替换插值表达式之后问题得到解决. 原因已经查明,因为第78行,定义对象car时后面 ...
- 记录python学习过程中的一些小心得
1.python中一切皆对象,内置数据结构也是对象.处理一个对象就是利用它带有的方法和属性,对该对象进行处理,一步步达到我们想要的结果. 2.编程时,先构思好我们处理的对象是什么,具有哪些属性和方法, ...
- JS学习过程中碰到的小问题
使用循环语句查找通讯录 //Setup var contacts = [ { "firstName": "Akira", "lastName" ...
- Java学习过程中的总结的小知识点(长期更新)
Java学习过程中的总结的小知识点 (主要是自己不会的知识和容易搞错的东西) 计算某个程序运行的时间 long stime=System.currentTimeMillis(); copy3(file ...
- Matlab中的一些小技巧
(转于它处,仅供参考) 1.. Ctrl+C 中断正在执行的操作 如果程序不小心进入死循环,或者计算时间太长,可以在命令窗口中使用Ctrl+c来中断.MATLAB这时可能正疲于应付,响应会有些滞后. ...
- MATLAB坐标系中绘制图片
MATLAB坐标系中绘制图片 方法一 使用图片坐标循环的方式,代码如下. clear,clc,close all tic; map=imbinarize(imread('map.bmp'));%map ...
- 关于JDBC学习过程中的注意事项(分享自己犯过的错误,写给初学JDBC的小伙伴的八条建议)
关于JDBC学习过程中的注意事项(分享自己犯过的错误,写给初学JDBC的小伙伴的八条建议) 前言:最近在学习JDBC,总结了几个小问题,特地分享给大家,让大家不要犯这样的错误,也希望大家养成学会总结的 ...
- Mybatis 学习过程中出现空指针异常的错误【已解决】
Mybatis 学习过程中出现空指针异常的错误[已解决] 以下是写的小测试的代码 bean层 Player类(篮球队队员) bean层 Team类(篮球队) dao层 TeamDao.xml配置文件 ...
- 一些JavaSE学习过程中的思路整理(主观性强,持续更新中...)
目录 一些JavaSE学习过程中的思路整理(主观性强,持续更新中...) Java书写规范 IDEA的一些常用快捷键 Java类中作为成员变量的类 Java源文件中只能有一个public类 Java中 ...
随机推荐
- oracle 用表连接替换EXISTS
通常来说 , 采用表连接的方式比EXISTS更有效率 SELECT ENAME FROM EMP E WHERE EXISTS (SELECT ‘X’ FROM DEPT WHERE DEPT_NO ...
- 洛谷 2403 [SDOI2010] 所驼门王的宝藏
题目描述 在宽广的非洲荒漠中,生活着一群勤劳勇敢的羊驼家族.被族人恭称为“先知”的Alpaca L. Sotomon是这个家族的领袖,外人也称其为“所驼门王”.所驼门王毕生致力于维护家族的安定与和谐, ...
- C# TransactionScope 事务类
微软自带的TransactionScope(.Net Framework 2之后)是个好东东,提供的功能也很强大. 首先说说TransactionScope是什么,并能为我们做什么事情.其实看Tran ...
- H3C PPP MP配置示例三
- JPA进行insert操作时会首先select吗
在某个项目中,使用JPA的saveAll方法去批量写入数据时,通过打印sql,发现每次insert前都会先select一次,极大的浪费了写入性能. 分析一下代码,saveAll() @Transact ...
- insert语句让我学会的两个MySQL函数
我们要保存数据到数据库,插入数据是必须的,但是在业务中可能会出于某种业务要求,要在数据库中设计唯一索引:这时如果不小心插入一条业务上已经存在同样key的数据时,就会出现异常. 大部分的需求要求我们出现 ...
- POJ 2763"Housewife Wind"(DFS序+树状数组+LCA)
传送门 •题意 一对夫妇居住在 xx村庄,给村庄有 $n$ 个小屋: 这 $n$ 个小屋之间有双向可达的道路,不会出现环,即所构成的图是个树: 从 $a_i$ 小屋到 $b_i$ 小屋需要花费 $w_ ...
- P1109 桃花岛
题目描述 不是任何人都可以进入桃花岛的,黄药师最讨厌象郭靖一样呆头呆脑的人.所以,他在桃花岛的唯一入口处修了一条小路,这条小路全部用正方形瓷砖铺设而成.有的瓷砖可以踩,我们认为是安全的,而有的瓷砖一踩 ...
- linux设备驱动文件结构
struct file, 定义于 <linux/fs.h>, 是设备驱动中第二个最重要的数据结构. 注意 file 与用户空间程序的 FILE 指针没有任何关系. 一个 FILE 定义在 ...
- linux 创建你的 /proc 文件
一旦你有一个定义好的 read_proc 函数, 你应当连接它到 /proc 层次中的一个入口项. 使用一个 creat_proc_read_entry 调用: struct proc_dir_ent ...