How can I convert a struct into a single string, with each element separated by a blank space character?
80 ビュー (過去 30 日間)
古いコメントを表示
MathWorks Support Team
2018 年 7 月 3 日
回答済み: MathWorks Support Team
2018 年 7 月 5 日
I have a 2x1 structure with 3 fields. All the elements are numeric. I want to convert this structure into a single character vector, with each element separated by a blank space character. How can I do this?
採用された回答
MathWorks Support Team
2018 年 7 月 3 日
This can be done using the following code:
%Create structure with 3 fields
cc
struc.a = 134;
struc.b = 2.324;
struc.c = 33.87;
struc(2).a = 48769;
struc(2).b = 56.543;
struc(2).c = 67.654;
struc
%struct to cell
cel = struct2cell(struc);
si = size(cel);
cel = reshape(cel,si(1),si(3));
%cell to matrix
mat = cell2mat(cel)
%matrix to str
strR = mat2str(mat);
%adding spaces and removing extra characters
strR = strrep(strR,';',' ');
strR = strrep(strR,'[','');
strR = strrep(strR,']','')
%The ordering will be row wise
0 件のコメント
その他の回答 (0 件)
参考
カテゴリ
Help Center および File Exchange で Cell Arrays についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!