eval function used in matlab

eval function used in matlab

Enter help eval in matlab command line window command Enter to see the official explanation of the eval function, probably means that the expression is executed in the matlab, calculate the code expression represented. Meaning that corresponds to the input mean expression expression command on the command line.
usage:

eval(expression)
[output1,…,outputN] = eval(expression)

Here it is very strange, why not enter commands directly from the command line but rather a multi-use eval function to do this sort of thing. This is a reason, the following example:
1, for example, you need to use the matlab load some data, and these data are stored in a number of files, the file name has certain rules, such as: data1.mat data2.mat .. .... and so on, what you need to load it? Is to write directly n-load data1.mat do, obviously not, and when this happens is that the eval function to show the time of its usefulness. You can write:

for i = 1:n
    eval(['load',' ','data',num2str(i),'.mat']);  % 这样完全跟上面的功能一样
end

2, when you need to extract a string of numbers in the string, or in the sequence of decimal numbers each number extracted, separated with a space between each number, i.e. a = '12 34 45 67 ' such a form you how to extract them into a matrix it? This can also use the eval function to solve this problem:

for i = 1:4
    num(i) = eval(['hex2dec(','a(3*i-2:3*i-1)',')']);  % 使用这个语句就可以实现将上面的字符串中的数字转换为相应的矩阵了
 end 

Note pit:

1, eval function is equivalent to input commands on the command line, so the form of the command to keep the form of expression must be consistent, otherwise return an error, this is the code above 1 Why do we need to use: eval ([ 'load', '', 'data', num2str (i) , '. mat']); also added in the middle of expression '', this is the need to maintain the correct expression.
2, since the eval will directly output the command, so that it can automatically recognize the actual value of the variables, the second input is the direct cause of the above code i of the expression: the eval ([ 'HEX2DEC (', 'A (. 3 I- 2:. 3 I-. 1) ',') ']);
. 3, if there is a symbol in the eval expression' when required i.e. escape characters required in the 'add two places'' can, so that he is a 'instead of a string that represents meaning.

Guess you like

Origin www.cnblogs.com/zhicungaoyuan-mingzhi/p/11871703.html