필터 지우기
필터 지우기

Print to file a cell array of struct

조회 수: 1 (최근 30일)
nedo nodo
nedo nodo 2013년 1월 14일
Hi all,
How can I write to file a cell of struct? For instance if I have
a=struct('first',1,'second',2);
b=struct('third',3,'fourth',4); myCell={a,b}.
end
I cannot use something like this since that it requires the redefinition of the function fprintf:
if true
fileID=fopen(fileName, 'w');
for l=1:size(myCell,1)
fprintf(fileID,'%s,%s',myCell{l});
end
fclose(fileID);
end
Thank you
  댓글 수: 1
Matt J
Matt J 2013년 1월 14일
Why not save to a .mat file?

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

채택된 답변

Thorsten
Thorsten 2013년 1월 21일
편집: Thorsten 2013년 1월 21일
save('myfile.mat', myCell)

추가 답변 (1개)

Nick
Nick 2013년 1월 21일
For writing structs, strings, cells to file I once made the following programm: if you want to read the data back out of the file use the command fopen('exp','r')
%Madup input value's
clear
%string
DataStruct.NUL = 'Header';
%long number with dots
DataStruct.FILEID = '2.0.0.0.0.0.1';
%array in struct one
DataStruct.EEN = [1 : 10];
%array in struct two
DataStruct.TWEE = [2 : 11];
%array in struct three
DataStruct.DRIE = [3 : 12];
%array in struct four
DataStruct.VIER = [4 : 13];
%array in struct five
DataStruct.VIJF = [5 : 14];
%Cell in struct
DataStruct.ZES = {5 : 14};
%string
DataStruct.CLOSE = 'Close';
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Write DataStruct to file
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%open a file to write your data to I named this exp
fid = fopen('exp','w');
NUL = DataStruct.NUL;
fprintf(fid, '%s\n', NUL);
FILEID = DataStruct.FILEID;
fprintf(fid, '%s\n', FILEID);
str = [];
for ii = 1:length(DataStruct.EEN)
str = [str, ' ', num2str(DataStruct.EEN(ii))];
end
EEN = num2str(str);
fprintf(fid, '%s\n', str);
str = [];
for ii = 1:length(DataStruct.TWEE)
str = [str, ' ', num2str(DataStruct.TWEE(ii))];
end
TWEE = num2str(str);
fprintf(fid, '%s\n', str);
str = [];
for ii = 1:length(DataStruct.DRIE)
str = [str, ' ', num2str(DataStruct.DRIE(ii))];
end
DRIE = num2str(str);
fprintf(fid, '%s\n', str);
str = [];
for ii = 1:length(DataStruct.VIER)
str = [str, ' ', num2str(DataStruct.VIER(ii))];
end
VIER = num2str(str);
fprintf(fid, '%s\n', str);
str = [];
for ii = 1:length(DataStruct.VIJF)
str = [str, ' ', num2str(DataStruct.VIJF(ii))];
end
VIJF = num2str(str);
fprintf(fid, '%s\n', str);
str = [];
for ii = 1:length(DataStruct.ZES)
str = [str, ' ', num2str(cell2mat(DataStruct.ZES(ii)))];
end
ZES = num2str(str);
fprintf(fid, '%s\n', str);
str = [];
CLOSE = DataStruct.CLOSE;
fprintf(fid, '%s\n', CLOSE);

카테고리

Help CenterFile Exchange에서 Characters and Strings에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by