フィルターのクリア

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 Center および File ExchangeCharacters and Strings についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by