RallyToMe
RallyToMe

Reputation: 105

Truncating a single table cell in Matlab

I would like to truncate/clean up a single cell of a matlab table, such as this:

important =

2×8 table

  Var3             Var5              Var6            Var7           Var8            Var9           Var10          Var11  

________    __________________    ___________    ____________    ___________    ____________    ____________    __________



09:13:30    'Zone="<0>"'    'Vset="19"'    'Vrdb="0.0"'    'Iset="10"'    'Irdb="0.0"'    'Pset="190"'    'Prdb="0"'

09:13:30    'Zone="<1>"'    'Vset="19"'    'Vrdb="0.0"'    'Iset="10"'    'Irdb="0.0"'    'Pset="190"'    'Prdb="0"'

I would like to be able to truncate Var5, to trim it down to just the number (1 or 0 in this case). I don't know if it would be best to pull out Var5 and modify it or something else.

Any guidance would be appreciated. Thanks

Upvotes: 0

Views: 78

Answers (1)

Luis Mendo
Luis Mendo

Reputation: 112749

Let's define

t = table;
t.Var5 = {'Zone="<0>"'; 'Zone="<1>"'};
t.Var6 = {'Vset="19"'; 'Vset="19"'};

Assuming that the number you want is just a sequence or digits (no sign, decimals etc), that each table entry contains only one such sequence (or you only want the first) and that you want the results as character vectors:

t.Var5 = regexp(t.Var5, '\d+', 'match', 'once');

Before:

 t =
  2×2 table
           Var5              Var6    
    __________________    ___________
    'Zone="<0>"'    'Vset="19"'
    'Zone="<1>"'    'Vset="19"'

After:

t =
  2×2 table
    Var5       Var6    
    ____    ___________
    '0'     'Vset="19"'
    '1'     'Vset="19"'

If you want the results as numbers:

t.Var5 = str2double(regexp(t.Var5, '\d+', 'match', 'once'));

After

t =
  2×2 table
    Var5       Var6    
    ____    ___________
    0       'Vset="19"'
    1       'Vset="19"'

Upvotes: 3

Related Questions