creating 3d array from images

47 views (last 30 days)
Xen on 9 Sep 2014
Commented: Xen on 9 Sep 2014
Hello, I am trying to read and copy a series of 5 images into a 3d array, where each image should have a 'thickness' of 13 pixels. When run I get this error:
Undefined function or variable 'B2'.
Error in U (line 20)
Why B1 image is copied, but not B2 (and I suppose the rest)? Here is the code. Thanks!
for i=1:5
x=[int2str(i) '.png'];
if exist(x, 'file')
B{i}=repmat(B{i},[1 1 n]);
fprintf('File %s does not exist.\n', x);

Accepted Answer

Geoff Hayes
Geoff Hayes on 9 Sep 2014
Xenios - I think that you mean to use B{2} in place of B2 (though I'm not sure how B1 passed - could you have defined it elsewhere?). Try replacing your line
Note how the above line also uses B{3} rather than C3.

More Answers (3)

David Young
David Young on 9 Sep 2014
B2 is the name of a variable, which has not been given a value. It appears that B1 was given a value in some code that isn't shown in your question. Anyway, these variables aren't used - instead you need to use the elements of the cell array B.
Assuming that C3 is just a typo, it looks like you mean, for the final line
I = cat(3, B{1}, B{2}, B{3}, B{4}, B{5});
  1 Comment
David Young
David Young on 9 Sep 2014
You may also want to consider putting something in the else part of the condition, for when a file is missing - e.g. B{i} = []. For generality and conciseness, you could also replace the final line with
I = cat(3, B{:});

Sign in to comment.

Xen on 9 Sep 2014
Thanks for the help guys! It solved the problem. Yes, C3 was a typo. It really is strange though, because B1 was not defined anywhere else, and this code works for
but not for the rest of any other Bi values. So I didn't assume that brackets are needed.
(Geoff got the accepted answer as he replied first)

Xen on 9 Sep 2014
By the way, what's the best way for including the I=cat(...) into the loop?
Xen on 9 Sep 2014
Hi. Thanks for that, it really works. This is not part of a huge code, so naming the variables is not too important! I will also pass the comments to the people who had suggested the use of these functions... Anyway, I apologise as I just started using MATLAB. This code is the only one that works as expected, so I am currently Ok with it! Hopefully all images are perfectly orderly named in their folder.

Sign in to comment.


Find more on Loops and Conditional Statements in Help Center and File Exchange

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by