How to use dlmwrite in a for loop?

10 views (last 30 days)
Hi everybody, I want to use dlmwrite to save data in .dat format for a cell array in the workspace.
Thats what I want obtain:
dlmwrite('Ejex1.dat', Ejex{1,1}','delimiter','\t','precision',16)
dlmwrite('Ejex2.dat', Ejex{1,2}','delimiter','\t','precision',16)
dlmwrite('Ejey1.dat', Ejey{1,1},'delimiter','\t','precision',16)
dlmwrite('Ejey2.dat', Ejey{1,2}','delimiter','\t','precision',16)
But the problem is when I try to put in a for loop, I tried many ways to obtain that but it doesn't work well:
for i=1:numfiles
NameFile(i)=filename(i)
dlmwrite('NameFile%d.dat',Ejex{1,i},'delimiter','\t','precision',16)
end
Another:
for i=1:numfiles
NameFile(i)=filename(i)
dlmwrite(NameFile.dat, Ejex{1,i},'delimiter','\t','precision',16)
end
Thanks in advance!! I hope you can help me

Accepted Answer

Joseph Cheng
Joseph Cheng on 18 Sep 2015
matlab is not going to fill in the %d number for you. you should be using something like sprintf to a variable to generate the filename for dlmwrite.
for i = 1:size(Ejex,1)
Xfilename = sprintf('Ejex%d.dat',i);
disp(Xfilename)%use xfilename for your dlmwrite
end

More Answers (0)

Categories

Find more on Data Import and Export 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!