Filter löschen
Filter löschen

Print to file a cell array of struct

1 Ansicht (letzte 30 Tage)
nedo nodo
nedo nodo am 14 Jan. 2013
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

Akzeptierte Antwort

Thorsten
Thorsten am 21 Jan. 2013
Bearbeitet: Thorsten am 21 Jan. 2013
save('myfile.mat', myCell)

Weitere Antworten (1)

Nick
Nick am 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);

Kategorien

Mehr zu Characters and Strings finden Sie in Help Center und File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by