0

In a directory I have a collection of 20 files. I want to:

  • iterate a loop across those 20 files,
  • extract x and y data from them and
  • place those data in a 2D matrix that is created within the loop

Note below that files' is a 20x1 struct and file is a 1x1 struct.

I'm unsure how to build the 2-dimenstional matrix A inside such a loop.

I've tried something like

files = dir('./cases/*.dcm');

for file = files'

    [data extraction here, creating vars x and y]

    for k = 1:length(files')
        A(k,:) =  (x:y);
    end
end

but I get

Subscripted assignment dimension mismatch.

Any idea what I'm doing wrong?

13
  • Any idea what I'm doing wrong?, Nope, as I can't see the sizes of all things from here. This is however one of the most clear error messages MATLAB can throw at you, meaning your RHS has a different number of elements than the LHS. Check the sizes of all things Commented Dec 14, 2015 at 14:59
  • Plus it's weird to iterate twice on the same stuff Commented Dec 14, 2015 at 15:00
  • yes i agree with both of you, but can you give me any pointer on how one would go about creating a 2D matrix in this situation? I'll be glad to provide any specific info you need, I just don't know where to begin. Commented Dec 14, 2015 at 15:01
  • What do your files contain exactly? What are the sizes of x and y? Commented Dec 14, 2015 at 15:03
  • 1
    Are you saying that X and Y are just one number each? If so try files = dir('./cases/*.dcm');for k = numel(files);file = files(k);[data extraction here, creating vars x and y];A(k,:) = [x,y];end. Commented Dec 14, 2015 at 15:18

1 Answer 1

1

This should work:

files = dir('./cases/*.dcm');
for k = 1:numel(files)
    file = files(k);
    %[data extraction here, creating vars x and y];
    A(k,:) = [x,y];
end

You might also want to add an initialization before the loop like:

A = zeros(numel(files),2);
Sign up to request clarification or add additional context in comments.

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.