MATLAB: Dump a struct to an ascii delimited file

write struct to file

Dear all, I have a code that looks like that
for i=1:10
mystruct.filename=sprintf('test %d',i);
mystruct.number=1;
end
and I want to dump these data in ascii file with comma or tab delimited Every line in the file I want to be one iteration so the file to look like
test1,1 test2,2 test3,3 …
How can I dump a struct to an ascii file?
Best Regards Alex

Best Answer

Hi,
only a guess:
for i=1:10
mystruct(i).filename=sprintf('test %d',i);
mystruct(i).number=i;
end
txt = struct2cell(arrayfun(@(x) structfun(@num2str,x,'UniformOutput',false),mystruct));
tmp = squeeze(strcat(txt(1,1,:),',',txt(2,1,:),';'));
fid = fopen('out.txt','w');
fprintf(fid,'%s',strcat(tmp{:}))
fclose(fid)
Related Question