MATLAB: File open and write issues

fileinputoutput

hi, I have k number of directories inside which there are l number of files. Now I want to open all the files one by one and write magic matrix of size l inside it. Now, everything's fine except when I try to write the file it does not work. Elements in the matrix are separated by '-' i.e. 1*3 matrix would simply look like 3-5-8. please help I dont know why I am getting an error on "fprintf(fid1,'%d-',magicmt(m,1:(l-1)))"
clc
clear all
nr_dir=input('Enter no. of directories to be created')
nr_magic=input('Enter the size for magic matrix')
%loop to create directory
for i=1:nr_dir
my_dir=strcat('dir',num2str(i))
m=(mkdir(my_dir)) %loop to create file
for j=1:nr_magic
file_str=strcat('file',num2str(j),'.txt')
full_file_str=strcat('dir',num2str(i),'\',file_str)
fid=fopen(full_file_str,'wt')
fclose(fid)
j=j+1
end
i=i+1
end
%%write to files
for k=1:nr_dir
for l=1:nr_magic
fid1=fopen(strcat('dir',k,'\','file',l,'.txt'))
magicmt=magic(l)
for m=1:size(magicmt,2) %through columns
fprintf(fid1,'%d-',magicmt(m,1:(l-1))); %place *** upto n-1th column only
fprintf(fid1,'%d',magicmt(m,l)) %nth column
fprintf(fid1,'\n'); %print new line
fclose(fid1)
end
l=l+1
end
k=k+1
end

Best Answer

Well you have a dash in there so of course it puts it in.
Replace
fprintf(fid1,'%d-',magicmt(m,1:(l-1)));
with
fprintf(fid1,'%d ',magicmt(m,1:(l-1)));
There is no dash after the %d, just a space, so your numbers will be separated by a space instead of a dash.