Lucky
Lucky

Reputation: 11

MATLAB code doesn't work when I run it as a loop?

I'm assigning some files to variables in MATLAB. I'm a little lazy and trying to maybe demonstrate a little problem-solving, so I tried to write a function to do this. The body of the function:

i=0
for i=0:8
    eval(sprintf('C%d=wavread([''C'' num2str(i)]);', i));
    eval(sprintf('Cs%d=wavread([''Cs'' num2str(i)]);', i));
    eval(sprintf('D%d=wavread([''D'' num2str(i)]);', i));
    eval(sprintf('Ef%d=wavread([''Ef'' num2str(i)]);', i));
    eval(sprintf('E%d=wavread([''E'' num2str(i)]);', i));
    eval(sprintf('F%d=wavread([''F'' num2str(i)]);', i));
    eval(sprintf('Fs%d=wavread([''Fs'' num2str(i)]);', i));
    eval(sprintf('G%d=wavread([''G'' num2str(i)]);', i));
    eval(sprintf('Af%d=wavread([''Af'' num2str(i)]);', i));
    eval(sprintf('A%d=wavread([''A'' num2str(i)]);', i));
    eval(sprintf('Bf%d=wavread([''Bf'' num2str(i)]);', i));
    eval(sprintf('B%d=wavread([''B'' num2str(i)]);', i));
    i=i+1
end

Everything's hunky dory when I just assign a value to i and run the code within the loop, but when I actually run it as a loop, it'll just run to completion without returning any variables.

Any ideas why?

Thanks y'all! Also figured out why my function didn't return anything! Stupid mistake :)

Upvotes: 0

Views: 413

Answers (2)

devrobf
devrobf

Reputation: 7213

I believe this is because eval generates its own workspace, so whilst the variables are being created, they are then lost at the end of the eval call, a bit like the way variables created inside a function are lost when it returns. I suggest you do this the 'proper' way and use cell arrays instead:

i = 0; % Note: uneccessary line!
for i = 0:8
    C{i + 1} = wavread(['C' num2str(i)]);
    Cs{i + 1} = wavread(['Cs' num2str(i)]);
    D{i + 1} = wavread(['D' num2str(i)]);
    Ef{i + 1} = wavread(['Ef' num2str(i)]);
    E{i + 1} = wavread(['E' num2str(i)]);
    F{i + 1} = wavread(['F' num2str(i)]);
    Fs{i + 1} = wavread(['Fs' num2str(i)]);
    G{i + 1} = wavread(['G' num2str(i)]);
    Af{i + 1} = wavread(['Af' num2str(i)]);
    A{i + 1} = wavread(['A' num2str(i)]);
    Bf{i + 1} = wavread(['Bf' num2str(i)]);
    B{i + 1} = wavread(['B' num2str(i)]);
    i = i+1;  % Note: uneccessary line!
end

EDIT: Ignore what I said about eval, see Dan's comment below. Nonetheless, a cell array is the appropriate way to tackle this.

Upvotes: 1

Dan
Dan

Reputation: 45741

If your code is the body of a function as you have said, then the code generated by eval will be scoped to that function. As soon as the function returns, the variables are released. The code posted by jazzbassrob will also not work if it's in a function, but it is definitely the correct way to go. Just make it a script and not a function. Or even better, make the function return the cell arrays:

function [C, Cs, ...] = readWavs()
    for ii = 0:8
        C{ii} = wavread(['C' num2str(ii)]);
        ....
        ii = ii + 1 %NB this line makes no sense! i will increment by 1 anyway because of the for loop. If you want to increment by 2 then you should change the loop to for ii = 0:2:8
    end
end

Or a struct of the cell arrays:

function wavStruct = readWavs()
    for ii = 0:8
        wavStruct{ii}.C = wavread(['C' num2str(ii)]);
        ...
    end
end

Also it's best not to use i or j as variable names in matlab as they are reserved for sqrt(-1), hence I've used ii

Upvotes: 0

Related Questions