2008年12月16日星期二

在MATLAB的GUI中区分str2double和str2num函数

有网友提示说在GUI的edit控件上,使用str2num函数会导致错误,我看了一下MATLAB帮助文档,其在edit控件上推荐使用str2double函数而不是str2num,具体我编了一个程序试验了一下,有兴趣的网友可以测试一下下面的代码:
a = '20'
b = {'10'}
a_num = str2num(a)
a_double = str2double(a)
try
b_num = str2num(b)
catch
b_double = str2double(b)
end

通过上面的代码我们可以清楚的了解str2double和str2num这两个函数的区别,其中,str2num不能转换cell元素,但是,str2double却可以胜任。
后来又专门查阅了str2double和str2num这两个函数的帮助,感觉似乎任何时候使用str2double都比较合适,各位网友不妨发表一下意见~

关于动态修改simulink模型参数的探讨

问题提出:

simulink运行到中间时刻,然后动态修改模型中的参数,然后再从断点开始接着运行

问题解决:

先说模块:如果仿真时simulink模块对话框里允许改动的,那么,就可以改,而且,改完之后,simulink似乎就认同这种改动,并按照改动后的数据继续仿真;不允许改动的当然就不行了,比如打开对话框之后是灰色的部分。

但是,这些的前提是不用命令修改,我发现,无论是增益(gain)模块还是Constant模块(其他未验证),如果里面写上变量名K,仿真前给K赋值为1,仿真暂停再赋值为10,继续仿真,似乎更改根本没有效果,仍然按照K=1进行仿真。而如果暂停时直接修改gain模块对话框里的值,就有效果。不知道我说清楚没有

再说函数:对于simulink调用m函数,也就是用MATLAB fun模块,在仿真进行过程中simulink支持m文件的任何改动,即使不暂停也可以,只要改动后保存,在仿真过程中立即生效;而对于embedded MATLAB function模块,就是嵌入到simulink内部的m函数,在仿真过程中语句是不能够改动的,暂停后,打开embedded MATLAB function模块,显示的是locked,也就是说仿真过程中不可更改;最后是s函数,如果在仿真中如果修改了s-function的m文件代码,那么对于仿真一点效果都没有,仿真仍然按照仿真开始时候的s-function的代码运行,除非是在没有仿真的时候里修改s-function的代码,否则没有效果。

也就是说,s-function应该是在仿真开始时就编译好了,中间修改是没有效果的;而一般的m函数可以直接修改,在仿真中将立即生效。

2008年11月27日星期四

让瞬间信号保持1s或更长时间的模块

某些时候,我们需要将一个瞬间出现的信号保持1s或其他的时间长度,比如将触发器触发获得的信号保持指定的时间长度,那么可以采用如下一种方法:
* 采用Monostable模块保持指定的时间长度;
* 在模型中放入一个Triggered Subsystem模块,用于保持这个瞬时信号;
* 在模型中放入一个Matlab function模块,用于将瞬时信号转化成正的标量;
* 用乘法器将Monostable模块的输出和Triggered Subsystem模块的输出相乘即可。



再封装一下,就实现了输入一个脉冲信号,输出一个宽度为1s的幅值为同样大小的方波信号。

2008年11月25日星期二

有趣的Matlab注释-自己写m文件函数帮助的技巧

每当我们help一个函数的时候,我们可以看到,Matlab会在最后加上See also,其实,自己写的m文件帮助也可以加上See also 后面带有链接这种功能的东西
如果在函数的第二行开始写注释,就可以实现用help function命令显示帮助信息的功能,这可能大家都知道了,就不多说了,关键是如何实现See also
See also 的具体实现不知道Matlab是怎么做到的,但是,只要我们注意一下写的格式,就可以写出See also来:
在See also FUNCTIONNAME的后面,一般是英文句号,其后一定要有空格,并且See also 必须这样写,不可以写成see also 或者别的什么,最重要的,See also 后面的函数名字要全部大写
function testHelp
%testHelp tests help
% You can get help from it
% See also TESTHELP.

出来的效果如下:
testHelp tests help
You can get help from it
See also testhelp.

可以看到,上面的testhelp被自动小写,而且加了链接
如果有一个字母没有大写,比如第一个t:

function testHelp
%testHelp tests help
% You can get help from it
% See also tESTHELP.


效果如下:
testHelp tests help
You can get help from it
See also testhelp.

可以看到,除了t之外,后面的esthelp被链接了
如果英文的句号后面没有空格:
function testHelp
%testHelp tests help
% You can get help from it
% See also TESTHELP.

效果如下:
testHelp tests help
You can get help from it
See also

这下一点也显示不出来了,很有意思,如果有多个函数需要see also,直接打逗号再写就可以了。

The Interesting Characters of Triggered Subsystem

Maybe sb. is not very familiar with Triggered Subsystem. In fact, there are many interesting characters in Triggered Subsystem.
This is the figure of Triggered Subsystems. One of its useful characters is that it keeps the value of the time when it has been triggered, until it is been triggered the next time. For example:Make the Trigger input of the Triggered Subsystem the Step signal, whose step time has been set to 1s. Thus the Triggered Subsystem would be triggered at 1 second. And the input of the empty Triggered Subsystem is Clock block. It is interesting to guess what is the output.
The answer is that at 1 second, the Triggered Subsystem keeps the value of that time until the simulation ended. As is shown in the below.
This is to say that at the time of simulation, we can use this model to save some value which we are interested in. Also, we can use the command of PERSISTENT to define the persistent variable in m-file to save some value while simulating.