1

I am new to MatLab so forgive my lack of knowledge here:

I want to iterate over multiple variables stored in a .mat file.

I have the following simple test code:

object = load('test.mat'); 

for name = fieldnames(object)
    disp(length(name))
    x = object.(name{1})(:,1);
    y = object.(name{1})(:,2);
    disp('LOOP')
end

disp('DONE')

Clearly, this code should load test.mat (which has two variables in it: test, and testCopy, which are 90x2 doubles). The for loop should, for each variable, assign the first column to X and the second column to y, and then display LOOP.

Since test.mat has two variables, I should display LOOP twice and DONE once. However, The output is as follows - it's not iterating!

>>program
 2

LOOP
DONE

I'm totally stumped - the program knows that the length of "name" is 2, and so the loop should be executed twice. What am I missing? Thanks in advance for your help.

Nate
  • 105
  • 1
  • 12
  • 1
    See: [`for`](https://www.mathworks.com/help/matlab/ref/for.html) documentation, particularly the third bullet under *Description*. MATLAB's loops are not like Python's. – sco1 Feb 12 '17 at 02:51

0 Answers0