0

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 :)

2
  • replace all your eval with disp and run the loop and see what's wrong with the code you are generating. This is the best way to see what's going wrong when using eval, but you should almost definitely not be using eval Commented Apr 27, 2013 at 21:50
  • Instead of eval you should make a cell array: stackoverflow.com/questions/16099398/… Commented Apr 27, 2013 at 21:51

2 Answers 2

1

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.

Sign up to request clarification or add additional context in comments.

3 Comments

I think you are wrong about eval, it can definitely create variables. But this is the correct way to go. It just must not be in a function, or that function must return all the variables.
You're right, it can. I was slightly jumping to conclusions after OP said "without returning any variables", but he obviously didn't mean that they didn't exist in the workspace.
No I think he does mean that, they won't exist in the workspace but not because of eval, because he's made a function that doesn't return them (i suspect). But +1 for the cell arrays
0

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.