Académique Documents
Professionnel Documents
Culture Documents
Stranica 1
Solutions
Academia
Support
User Community
Company
Product Support
1103 - What Is the EVAL Function, When Should I Use It, and How Can I Avoid It?
What is the EVAL function? What problems can occur when I use the EVAL function? Example 1: Saving data to incrementally numbered ASCII files Example 2: Load data from the ASCII files generated in Example 1 Example 3: Using EVAL to print to random files Example 4: Reassign the contents of a variable Example 5: Evaluate a MATLAB command Example 6: Converting a numeric string to its numeric value Example 7: Performing error handling Closing remarks on the examples Functions similar to EVAL
EVAL can be used to alter arbitrary variables. In addition, two related functions which will be described in Section 11, EVALIN and ASSIGNIN , can be used to alter variables in different function workspaces. These functions can create bugs which are difficult to reproduce and nearly impossible to eliminate. EVAL is not part of the Embedded MATLAB Subset and as such can not be used when generating C code. When using the MATLAB Compiler,
functions that are called explicitly from your main MATLAB file are automatically included by MATLAB Compiler; however, functions that are not explicitly called, for example through EVAL , need to be included at compilation using the -a switch of the mcc command. Below are some basic examples of how to use EVAL , along with examples which avoid using EVAL but perform the same tasks. These examples will provide you with the ability to create just about any expression in MATLAB.
rootname = 'file'; % Root filename extension = '.dat'; % Extension for the files % The following loop concatenates the root filename, % an integer value, and the extension to create the % file in which the data is saved. for data = 1:10 filename = [rootname, num2str(data), extension]; eval(['save ', filename , ' data -ascii']) end
NON-EVAL CODE:
%Use the functional form of SAVE rootname = 'file'; % Root filename extension = '.dat'; % Extension for the files % The following loop concatenates the root filename, % an integer value, and the extension to create the % file in which the data is saved. for data = 1:10 filename = [rootname, int2str(data), extension];
http://www.mathworks.com/support/tech-notes/1100/1103.html
21.1.2011. 9:55:39
Support - What Is the EVAL Function, When Should I Use It, and How Can I Avoid It?
save(filename,'data','-ascii') end Example 2: Load data from the ASCII files generated in Example 1
EVAL CODE:
Stranica 2
rootname = 'file'; % Root filename extension = '.dat'; % Extension of the files % The following loop concatenates the root filename, % an integer value, and the extension to create the % file to be loaded. for data = 1:10 variable = [rootname, int2str(data)]; filename = [variable, extension]; eval(['load ', filename]) eval(['data', num2str(data), ' = ', variable, ';']) eval(['clear ', variable]) end
This section of code has slightly different functionality from the previous. NON-EVAL CODE:
rootname = 'file'; % Root filename extension = '.dat'; % Extension of the files %Here we assign all of the values loaded into the %array called "var". This allows us to avoid the %EVAL commands. for data = 1:10 filename = [rootname,num2str(data), extension]; variable = load(filename); var(data) = variable; clear variable end
rootname = 'fig'; for x = 1:10 figure(x) plot(rand(x)) filename = [rootname, int2str(x)]; % root filename and % the integer. eval(['print -dps ',filename]) % using EVAL end
NON-EVAL CODE:
% Root filename
%Use the functional form of PRINT rootname = 'fig'; for x = 1:10 figure(x) plot(rand(x)) filename = [rootname, int2str(x)]; % root filename and % the integer. print(filename,'-dps') % w/o using EVAL end
% Root filename
fun1.m
----------------->
http://www.mathworks.com/support/tech-notes/1100/1103.html
21.1.2011. 9:55:39
Support - What Is the EVAL Function, When Should I Use It, and How Can I Avoid It?
% Enter 'var' as the variable name. variablename = input('Enter the name of the variable: a = eval(variablename); % Reassign the contents of % variablename to a. b = 2*a; mesh(b)
Stranica 3
','s');
num = input('Enter the numerator: '); den = input('Enter the denominator: '); eval(['bode(',mat2str(num),',',mat2str(den),')'])
NON-EVAL CODE:
%Use FEVAL fun1 = 'bode'; num = input('Enter the numerator: '); den = input('Enter the denominator: '); feval(fun1,num,den)
EVAL CODE:
% CASE 1: A is not defined clear all eval('B = A','disp(''A is undefined'')') % The message "A is undefined" is displayed % CASE 2: A is defined A = 1; eval('B = A','disp(''A is undefined'')') % The following is displayed in the command window: % % B = % 1
NON-EVAL CODE:
%Use TRY - CATCH % CASE 1: A is not defined clear all try B = A catch disp('A is undefined') end % The message "A is undefined" is displayed % CASE 2: A is defined A = 1; try B = A catch disp('A is undefined') end % The following is displayed in the command window:
http://www.mathworks.com/support/tech-notes/1100/1103.html
21.1.2011. 9:55:39
Support - What Is the EVAL Function, When Should I Use It, and How Can I Avoid It?
% % B = %
Stranica 4
eval('disp(''''''This is a string'''''')')
displays 'This is a string' in the command window.
There is one trap of which you should be aware, which is using the EVAL statement when it is not necessary. For example:
title(['Plot #',num2str(1)])
The latter is much easier to read and write.
feval('bode',num,den)
The first input is the name of the function to be evaluated, and subsequent inputs are the inputs to the function. As you can see, this is clearer than:
eval(['bode(',mat/str(num),',',mat2str(den),')']) EVALIN is used in the same manner as EVAL , except that it has an additional argument which allows you to specify in which workspace the expression you pass to it should be evaluated. It can be used to pass results from one function to the workspace of the function which called it or to the base MATLAB workspace, which can be useful if you require information from a subfunction for another function.
ASSIGNIN allocates to a variable whose name you specify a certain value. The variable is created (or overwritten) in either the workspace of the function which called the function containing ASSIGNIN or the base workspace. With ASSIGNIN , as with EVAL and EVALIN, it is easy to create very subtle and hard to eliminate bugs by changing values of variables in an entirely different function from the one in which they were created. UNIX and DOS are used to open UNIX or DOS shells to execute the given command. For example: file = input('Enter the file name and extension: ','s'); unix(['lpr -Pprinter ',file]) dos(['copy /b ',file', lpt1'])
This can be rewritten using eval:
file = input('Enter the filename and extension: ','s'); eval(['!lpr -Pprinter ',file]) eval(['!copy /b ',file,' lpt1'])
The only difference between using UNIX or DOS and using EVAL is that with EVAL you must use the bang function (!) to execute a shell command. The UNIX and DOS commands do this automatically, so it is best to use these commands when shelling out to the operating system.
http://www.mathworks.com/support/tech-notes/1100/1103.html
21.1.2011. 9:55:39
Support - What Is the EVAL Function, When Should I Use It, and How Can I Avoid It?
1994-2011 The MathWorks, Inc. - Site Help - Patents - Trademarks - Privacy Policy - Preventing Piracy - RSS
Stranica 5
http://www.mathworks.com/support/tech-notes/1100/1103.html
21.1.2011. 9:55:39