Stuart’s MATLAB Videos

Watch and Learn

Building a Matrix in a For Loop 5

Posted by Stuart McGarrity,

On an existing post, a MATLAB user asked how to vertically concatenate a number of matrices taken from MAT-files. Here is a code-along of me working through a couple of options for this.

Play the video in full screen mode for a better viewing experience.

5 CommentsOldest to Newest

Peter Royds replied on : 1 of 5
Nice demo. To avoid a cumbersome 'if/else' statement in the simple method, I like to assign an empty matrix to result before the loop: result = []; for k = 1:length(fileInfo) ... result = [result; x]; etc.
Stuart McGarrity replied on : 2 of 5
That's cleaner and more efficient. I think I started using the if/then/else way because I had a vague memory of an edge case where the concatenation of an empty variable with a non-empty one did not work. I can't reproduce that so I have been imagining it.
Stuart McGarrity replied on : 4 of 5
I don't recall the MAT file object being faster than the load function. The MAT file object does allow you to access parts of an array in a MAT file, but I assume the user here wants to load all the data.
Jan Afridi replied on : 5 of 5
l Thank you, sir, for your wonderful tips! we learned a lot from you. I also publish an article about for loop . Thank you may like work…