% wjlljw@gmail.com]wjlljw@gmail.com
% hustwjl@qq.com]hustwjl@qq.com
% alljoyland
% see also
% evalc, evalin, assignin, feval, catch, lasterror, try
% 注意一个是 feval 是 function eval , f 未写在后边
\\\\\\\\
eval 高级语法使用,
详细讲解 eval用法
字符串构造
以及 帮助的翻译
\\\\\\\\\\\\\\\\\\\\\\\\\\
eval
执行字符串表达式
两步
1 构造字符串
2 执行字符串
\\\\\\\\\\\\\\\\\\\\
构造字符串技巧
num2str
可以变数字为字符串
\\\\\\\\\\\\\\\\\\\\\\\\
\\\\\\\\\\\\\\
常见的应用
1 自动保存文件
2 自动生成变量
3 内容不定问题
\\\\\\\\\\\\\\\\
\\\\\\\\\\\
细节的问题
1) 必须是字符串
2) 单引号 是 '''' 单引号字符串
3) 特殊的 不妨用 char
4) [] 就可以连接字符串
\\\\\\\\\\\\\\\\\\\\\\\\\
\\\\\\\字符串连接
str0= [ str1,str2]
以下的例子 并不能运行,因为是摘录代码,仅供参考,
也未删节,但加注释
\\\\\\\\\\\\\\\\
四个例子
\\\\\\\\\\\\\
ex1
n1 = 10;
% [yy,zz,xx] = meshgrid( 0:(l2-0)/(n1-1):l2 , 0:(l3-0)/(n1-1):l3,l1 / 2 );
xx = [ .2 .3 .25 .25 .2 .4 .25 .2 .2 .3];
yy = [ .2 .2 .2 .2 .1 .2 .2 .1 .3 .2];
zz = [ .15 .15 .2 .1 .2 .15 .15 .15 .15 .2];
%%
% a test
% i1 = 3
% i2 =3
% pos = [xx(i1,i2),yy(i1,i2),zz(i1,i2)]
for i1 = 1:n1
pos = [xx(i1),yy(i1),zz(i1)];
ff1 = 0:1:630;
pxw = zeros(size(ff1));
for i =1:length(ff1)
pxw(i)= cal_pxw(ff1(i),pos,model);
end
l_pxw = 20*log10(abs(pxw)/(2e-5))-30;
save_str_mat = ['save',' ','.\orgindata\p_',num2str(i1),'_spl',' ','l_pxw'];
% 构造字符串save 路径 文件名(加了数字,自动把循环代码加进去了)
% 原始字符串 save 空格 .\origindata\p_数字_spl 空格 l_pxw
eval(save_str_mat);
% 执行字符串
end
ex2
来源于 cae的软件的脚本
comsol35
textabel
function texcode=textable(V,varargin)
row1=[row1,sprintf(' \\\\ \\hline \\hline')];
texcode=strvcat(texcode,row1);
end
for j=1:nrow;
if ~isempty(opt.ylabel)
rowj=[opt.ylabel{j},' & '];
else
rowj=[];
end
rowj=[rowj,'%.',num2str(opt.decimals),'f'];
for i=2:ncol;
rowj=[rowj,' & %.',num2str(opt.decimals),'f'];
% bywjllljw@gmail.comalljoyland
% 注意 num2str 的使用方法
end;
rowj=[rowj,' \\\\'];
texcode=strvcat(texcode,sprintf(rowj,V(j,:)));
end;
texcode=strvcat(texcode,sprintf('\\hline '));
texcode=strvcat(texcode,sprintf('\\end{tabular} '));
if ~isempty(opt.filename)
eval(['fid=fopen(''',opt.filename,'.tex'',''w'')']);
% 注意这里 ,执行了 复制表达式, 注意单引号 '''',
% 读这个句子,注意 逗号的分隔符
% fid = fopen('opt.filename.tex
% [ 'fid=fopen(''' , opt.filename, '.tex'' , ''w'')' ]
% 凡逗号分开,便是上句注释,显然有错, 因为'.tex'' 是错误的
%这三句 是 去掉, otp.filename的执行效果, 注意了,为了不调试,而去掉的,反正是字符串
% ['fid=fopen(''','.tex'',''w'')']
%ans =
%fid=fopen('.tex','w')
% 所以看懂这个句子,凡逗号分开是行不通的,因为逗号也在构造之列
% [ 'fid=fopen(''' , opt.filename , '.tex'',''w'')' ]
% 这样子 才是正确的断句
% 1) 基本上逗号分开
% 2) 单引号是 双单引号(在引号内), 如'fid=fopen('''
% 3) 当有单引号之内的双引号, 处的逗号就不是分隔符了, 凡双引号,替换为单引号
% 4) 也就是说 单引号 先配对, 双引号,替换为单引号, 不是所有的都好都是分隔符
例如
'.tex'',''w'')'
ans =
.tex','w')
%
% 再 重复一遍
% 逗号分隔是因为 [ , , ,] 字符串连接的格式, 所以注意断句, 但是要区分假的逗号,即 作为构造的字符串本有的逗
号
% 本有的逗号, 分两种情况判别,一种是 , 单引号之内的; 另一种是靠语法判断, 比如 , fopen('file','w')
% 所以可以猜的必然有一个逗号,四个单引号
% 其中两个在 逗号旁边, 故此是 '','' → 等效为 ','
% 此外不再详述
for j=1:size(texcode,1);
rowj=texcode(j,:);
rowj=strrep(rowj,'\','\\');
fprintf(fid,[rowj,' \n']);
end
fclose(fid);
end;
ex3
comsol35
function texcode=texmatrix(V,varargin)
if ~isempty(opt.filename)
eval(['fid=fopen(''',opt.filename,'.tex'',''w'')']);
% 这个例子与上一个 是同样的
for j=1:size(texcode,1);
rowj=texcode(j,:);
rowj=strrep(rowj,'\','\\');
fprintf(fid,[rowj,' \n']);
end
fclose(fid);
end;
ex4
matlab
display
function display(this)
% display UNITGAIN objects.
% Copyright 2005-2006 The MathWorks, Inc.
% $Revision: 1.1.6.1 $ $Date: 2007/06/07 14:44:44 $
% Author(s): Qinghua Zhang
ObjectName = getDisplayName(this);
if isscalar(this)
disp([ObjectName ' object (no property).'])
else
S = size(this);
m = metaclass(this);
P = eval([m.Name,'.getListOfVisibleProperties(m);']);
% 这个例子
% 同样用了 [] 连接字符串
% 但是很显然简单多了
% 原意是 P= m.Name.xx(m);
% 凡属是 表达式的执行有变化的东西, 在执行之前不确定的时候,可以使用 eval
% 也即是说 模式(pattern 或 template,) 但是具体内容不定
if ~isempty(P)
disp(sprintf('%sx%s array of %s objects with following fields:',...
num2str(S(1)),num2str(S(2)),ObjectName))
disp(P)
else
disp(sprintf('%sx%s array of Custom Network objects.',...
num2str(S(1)),num2str(S(2))))
end
end
% FILE END
\\\\\\\\\\\\\\\\
\\\\\\\\\\\\\\\
附上帮助原文
eval
Execute string containing MATLAB expression
Syntax
eval(expression)
[a1, a2, a3, ...] = eval('myfun(b1, b2, b3, ...)')
执行不赋值表达式
执行表达式并赋值
详细说明
Description
eval(expression) executes expression, a string containing any valid MATLAB expression. You can construct
expression by concatenating substrings and variables inside square brackets:
注意,可以是any valid, 也就是功能非常强大,类似Java 或脚本的 eval函数
% int2str 整数的
% num2str 可以更好些
expression = [string1, int2str(var), string2, ...]
[a1, a2, a3, ...] = eval('myfun(b1, b2, b3, ...)') executes function smyfun with arguments b1, b2, b3,
..., and returns the results in the specified output variables.
Remarks
Using the eval output argument list is recommended over including the output arguments in the expression
string. The first syntax below avoids strict checking by the MATLAB parser and can produce untrapped
errors and other unexpected behavior. Use the second syntax instead:
% Not recommended
eval('[a1, a2, a3, ...] = function(var)')
% Recommended syntax
[a1, a2, a3, ...] = eval('function(var)')
同样是赋值
推荐后面一种,前一种可以但是不推荐
因为第一个 无法跟踪错误
相当于语法检查不严格,(可能是实现上的问题)
文件操作
Examples
Example 1 – Working with a Series of Files
Load MAT-files August1.mat to August10.mat into the MATLAB workspace:
for d=1:10
s = ['load August' int2str(d) '.mat']
eval(s)
end
These are the strings being evaluated:
s =
load August1.mat
s =
load August2.mat
s =
load August3.mat
- etc. -
生成变量名字
Example 2 – Assigning to Variables with Generated Names
Generate variable names that are unique in the MATLAB workspace and assign a value to each using eval:
for k = 1:5
t = clock;
pause(uint8(rand * 10));
v = genvarname('time_elapsed', who);
eval([v ' = etime(clock,t)'])
end
注意了 genvarname 同样非常有用
As this code runs, eval creates a unique statement for each assignment:
time_elapsed =
5.0070
time_elapsed1 =
2.0030
time_elapsed2 =
7.0010
time_elapsed3 =
8.0010
time_elapsed4 =
3.0040
执行返回函数名
Example 3 – Evaluating a Returned Function Name
The following command removes a figure by evaluating its CloseRequestFcn property as returned by get.
eval(get(h,'CloseRequestFcn'))
当内容不确定的时候