MATLAB: Debugging Help – Write a series of filenames

array

I'm writing a series of filenames to an array but am having issues with size mismatching.
q=[1:1000];
for n=1:1000
myfile(n)=sprintf('data_%06d.dat',q(n))
end

Best Answer

Hi Cameron,
If I try to run the same code, I get the "Subscripted assignment dimension mismatch.". The problem is in the assignment to myfile. The return of the sprintf (for any N) is a character array of length 15 - so one row and fifteen columns. When assigning this to the myfile matrix, the code requires more than just the row, but the columns for which the sprintf result is destined to:
myfile(n,:)=sprintf('data_%06d.dat',q(n));
Here the colon just means "all columns". Try this and see what happens. (Also consider pre-allocating memory for the myfile matrix.)
Geoff