Print to file a cell array of struct

15 次查看(过去 30 天)
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

采纳的回答

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 中查找有关 Workspace Variables and MAT Files 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by