필터 지우기
필터 지우기

Dump a struct to an ascii delimited file

조회 수: 3 (최근 30일)
Alex
Alex 2011년 8월 16일
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
Friedrich
Friedrich 2011년 8월 16일
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.

댓글을 달려면 로그인하십시오.

채택된 답변

Friedrich
Friedrich 2011년 8월 16일
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
Friedrich
Friedrich 2011년 8월 18일
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);
Alex
Alex 2011년 9월 2일
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

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Structures에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by