eval() command in Matlab

22,360

Solution 1

The eval function evaluates the given string as a MATLAB expression; so, for example

eval('x=1')

Would set variable x to 1. In this case it is being used to call wavread on different inputs. wavread is a function which reads a WAV audiofile, so for example the statement:

x=wavread('ti_00apple01.wav.wav');

reads the WAV file 'ti_00apple01.wav.wav' into the variable x.

Solution 2

It justs reads the ti_0dapple01.wav.wav file into the variable x with warhead, where d is one of the digits in "digits". eval evaluates ("runs") the code in the input string.

Why the code is not just:

 x=wavread(sprintf('ti_0%dapple01.wav.wav',d)); 

is strange.

Share:
22,360
lara
Author by

lara

Updated on October 16, 2020

Comments

  • lara
    lara over 3 years

    I am a beginner in Matlab coding. I was reading a code which I got from net about speech recognition. The .m file in which audio wave is loading has the code like this

    clc;
    name=['S5T0','S5T1','S6T0','S6T1','S7T0','S7T1','S8T0','S8T1','S9T0','S9T1']; %
    digit=['0123456789']; %
    
    for d=1:length(digit)
        eval(['x=wavread(''ti_0',digit(d),'apple01.wav.wav'');']);
        for k=1:4:length(name)
              [x1 x2]=vadnew(x);
              eval(['ti_0',digit(d),'F3',name(k:k+3),'=x(100*x1:100*x2);']);  
                               %ti_00F3S0T0=wavread('x(1).wav');
              x=x(x2*100:length(x));
        end
    end
    save ('F3new.mat', 'ti*');
    

    Can someone please tell me what this line is doing?:

    eval(['x=wavread(''ti_0',digit(d),'apple01.wav.wav'');']);