Print to file a cell array of struct

5 views (last 30 days)
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

Accepted Answer

Thorsten
Thorsten on 21 Jan 2013
Edited: Thorsten on 21 Jan 2013
save('myfile.mat', myCell)

More Answers (1)

Nick
Nick on 21 Jan 2013
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);

Categories

Find more on Characters and Strings in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!