mat
mat

Reputation: 2617

Preserve blank space while using cellstr

I'm trying to store a series of formated numbers as a strings in a table and I need to preserve all the white spaces. I don't know if there's a better way to store strings in a table (any recommendation would be appriciated) but this is what I'm using.

% Initialize table
mytable = array2table(cell(5,5));

% Variables
a = 0.04;

I want to store '0.04 ' (with 2 blank spaces at the end) in the first cell of mytable. This is what I tried:

mytable{1,1} = cellstr([num2str(a), '  ']);

However, I know that cellstr() doesn't preserve white spaces. I don't know what function to use to store the variables. I tried with char() but I'm getting errors. Thank you!

Upvotes: 0

Views: 237

Answers (1)

Benoit_11
Benoit_11

Reputation: 13945

You might want to try strcat:

mytable{1,1} = strcat(num2str(a),{'  '})

Which gives this output:

mytable = 

      Var1      Var2    Var3    Var4    Var5
    ________    ____    ____    ____    ____

    '0.04  '    []      []      []      []  
    []          []      []      []      []  
    []          []      []      []      []  
    []          []      []      []      []  
    []          []      []      []      []  

Upvotes: 2

Related Questions