Dump a struct to an ascii delimited 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

1 Comment

Are you sure that your code looks like this? Since the above code will result in one struct with
filename: 'test 10'
number: 1
as values/fields.

Sign in to comment.

 Accepted 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)

7 Comments

Ok my bad!
I was trying to keep it simple!
my struct is an array of items. So Friedrichs answer works.
Just a small change
txt = struct2cell(arrayfun(@(x) structfun(@num2str,x,'UniformOutput',false),s));
%
a = squeeze(txt);
size(a)
ans =
19 138
as you can see I converted this struct to matrix so I want to
dump this matrix to a file by using semicolon as a delimeter.
I want to store the matrix to the file by keeping its structure as it is.
Have a nice evening
Best Regards
Alex
Any update on this?
In brief I want to dump a [19,138] matrix to a file
by writing
19 rows in a txt file
and having all the columns (138) with comma delimited.
B.R
Alex
Can you give us details about the exact data structure? If its really a matrix you can use dlmwrite to put it into a file in one go. But I doubt this will work in your case.
It was a struct converted to a matrix like struct by using
txt = struct2cell(arrayfun(@(x) structfun(@num2str,x,'UniformOutput',false),s));
There are cells that contain string and few that contain numbers.
That helps?
Not really, can you post a small version of your variable s?
Another guess. I created a dummy cell a which has some rows and columns:
a = {'abcde', 'fghi', 'jk','l'; '1','2','3','4'};
[n m] = size(a);
b = cell(n,2*m);
b(:,1:2:2*m-1) = a;
b(:,2:2:end) = {','};
b(:,end) = {';'};
fid = fopen('out.txt','w');
for i=1:n
fprintf(fid,'%s \n',strcat(b{i,:}));
end
fclose(fid);
Ok this one worked for me!!!!
txt = struct2cell(arrayfun(@(x) structfun(@num2str,x,'UniformOutput',false),s));
for k=1:length(s) % Dump rows to the file. Every struct entry is a line into the file
formRow=squeeze((strcat(txt(:,:,1),';')));
fid = fopen('measurementLogFiles.txt','a');
fprintf(fid,'%s',formRow{:});
fclose(fid);
end
My problem is that I want now to write the new rows into a new lines.
As the code is righ now every row is appended to the right of the last one. Unfortunately I can not split that into many new lines in excel.
Could you please help me append every measurement with a cr and lf?
I would like to thank you in advance for your help
B.R
Alex

Sign in to comment.

More Answers (0)

Asked:

on 16 Aug 2011

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!