Home > vbmeg > functions > common > utility > vb_struct2str.m

vb_struct2str

PURPOSE ^

SYNOPSIS ^

function txt = vb_struct2str(S)

DESCRIPTION ^

 構造体を文字列セル配列に変換
 2003-04-30 Taku Yoshioka
 2006-05-18 rhayashi : output formatted strings.
                       adapt to structure including cell array.

 Copyright (C) 2011, ATR All Rights Reserved.
 License : New BSD License(see VBMEG_LICENSE.txt)

CROSS-REFERENCE INFORMATION ^

This function calls: This function is called by:

SOURCE CODE ^

0001 function txt = vb_struct2str(S)
0002 %
0003 % 構造体を文字列セル配列に変換
0004 % 2003-04-30 Taku Yoshioka
0005 % 2006-05-18 rhayashi : output formatted strings.
0006 %                       adapt to structure including cell array.
0007 %
0008 % Copyright (C) 2011, ATR All Rights Reserved.
0009 % License : New BSD License(see VBMEG_LICENSE.txt)
0010 
0011 txt = fieldnames(S);
0012 
0013 names = fieldnames(S);
0014 Nmembers = length(names);
0015 
0016 % get max string length of struct member
0017 max_name_len = 0;
0018 for k=1:Nmembers
0019     if max_name_len < length(names{k})
0020         max_name_len = length(names{k});
0021     end
0022 end
0023 
0024 % member name space padding(using max_name_len)
0025 format = ['%' num2str(max_name_len) 's: %s'];
0026 
0027 for i = 1:length(txt);
0028     member = S.(names{i});
0029     % cell array
0030     if iscell(member)
0031         cell_str = '{';
0032         for k=1:length(member)
0033             child_member = member{k};
0034             if iscell(child_member)
0035                 % over level2 cell will not be expanded.
0036                 cell_str = [cell_str, ' ', '''{ cell }'''];
0037             else
0038                 % cell array strings connection
0039                 cell_str = [cell_str, ' ', '''', num2str(child_member), '''']; 
0040             end
0041         end
0042         cell_str = [cell_str, ' }'];
0043         txt{i} = sprintf(format, names{i}, cell_str);
0044     else
0045         if isstruct(member), continue; end
0046         % character or number
0047         txt{i} = sprintf(format, names{i}, num2str(member));
0048     end
0049 end;

Generated on Mon 22-May-2023 06:53:56 by m2html © 2005