matlab中eval函數的用法

最進看大牛程序有一段 express

 

eval(['temp(:,:,ori) = bg_r' num2str(r) '{' num2str(ori) '};']);

 

 

eval函數是以字符串的形式運行代碼
函數

下面是matlab官方的註釋:lua

eval(expression) evaluates the MATLAB code in the string expression. If you use eval within an anonymous function, nested function, or function that contains a nested function, the evaluated expression cannot create a variable.
 

其中一個好處就是假如我要對a1,a2,a3,a4,……,a100分別賦予1,2,3,……,100。這時eval就發揮做用了。
code

 for i=1:100
     eval(['a' num2str(i) '=' num2str(i)]);
 end圖片

固然還有其餘例子,好比批量存數據或圖片文件等等。
字符串

那麼開始提到的例子也就好解釋了。input

注意eval中的中括號在兩個以上字符串出現時必定要有,起鏈接做用。
string

如:
input:[‘hello’ ‘world’]
it

output:helloworld
io

相關文章
相關標籤/搜索