How to convert matlab table [Inf], '' entry to char string -


i have matlab table , want create sql insert statement of line(s).

k>> obj.conditiontable  obj.conditiontable =         name              data            category         description ________________    ____________    _________________    ___________  'layout'            'str'           ''                   ''          'radius'            [       inf]    'radius_2000_inf'    ''          'aq'                [         0]    '0'                  ''          'vehiclespeed'      [       200]    'speed_160_230'      ''          

erros when conditiontable = obj.conditiontable(1,:);

k>> char(conditiontable.data) error using char cell elements must character arrays.  k>> char(conditiontable.description)     ans =        empty matrix: 1-by-0 
  1. problem: [inf] entry
  2. problem: possibly [123] number entries
  3. problem: '' entries

additionally, following commands useless in matter:

k>> length(conditiontable.data)     ans =          1  k>> isempty(conditiontable.description)     ans =          0 

target statement this:

insert `conditiontable` (`name`, `data`, `category`, `description`, `etfmiso_id`) values ("layout", "str", "", "", 618); 

yes, num2str accept single variable of type , return string, these operations valid:

>> num2str('123') ans = 123 >> num2str('chop') ans = chop >> num2str(inf) ans = inf 

however, can deal purely numeric arrays (e.g. num2str([5 456]) valid), bomb out if try throw cell array @ (even if cells numeric).


there 2 possible way work around convert values character arrays:

1) use intermediate cell array
recreated table [t] same data in example. running:

%% intermediate cell array t3 = cell2table( cellfun( @num2str , table2cell(t) , 'uni',0) ) ; t3.properties.variablenames = t.properties.variablenames  t3 =           name         data         category         description     ______________    _____    _________________    ___________     'layout'          'str'    ''                   ''              'radius'          'inf'    'radius_2000_inf'    ''              'aq'              '0'      '0'                  ''              'vehiclespeed'    '200'    'speed_160_230'      ''  

produces new table containing strings. notice had recreate column names (copied initial table), these not transferred cell array during conversion. these method suitable relatively small tables, round trip table/cellarray/table plus call cellfun quite slow larger tables.


2) use varfun function

varfun tables equivalent of cellfun cell arrays. you'd think simple

t2 = varfun( @num2str , t ) 

would job ... no. error too. if @ varfun code @ line indicated error, you'll notice internally, data in table converted cell arrays , function applied that. saw above, num2str errors when met cell array. trick overcome that, send customised version of num2str accept cell arrays. example:

cellnum2str = @(x) cellfun(@num2str,x,'uni',0) 

armed that, can use convert table:

%% use "varfun" cellnum2str = @(x) cellfun(@num2str,x,'uni',0) ; t2 = varfun( cellnum2str , t ) ; t2.properties.variablenames = t.properties.variablenames ; 

this produce same table in example 1 above. notice again had reassign column headers on newly created table (the irony varfun choked trying apply function on column headers, not re-use or return them in output ... go figure.)


discussion: tried make varfun solution work (hence t2 name of result), , wanted recommend one, because didn't table/cell/table conversion of other solution. have seen goes on varfun, not sure solution faster. might more readable in semantic way, if speed concern you'll have try both version , choose 1 gives best results.


Comments

Popular posts from this blog

magento2 - Magento 2 admin grid add filter to collection -

Android volley - avoid multiple requests of the same kind to the server? -

Combining PHP Registration and Login into one class with multiple functions in one PHP file -