matlabmatlab-table

write matrix to one column of a table - matlab


>> x = 'x';
>> y = [1,2,3,4];
>> T = table({x},{y});
>> writetable(T,'T.txt','Delimiter','\t')

when I open T.txt the different elements of y are written in separate columns:

Var1    Var2_1  Var2_2  Var2_3  Var2_4
x          1       2       3      4

is there a way to put them in one single column? eg:

Var1  Var2
x     [1,2,3,4]

Solution

  • For creating that table, you can make Var2 a character array instead. Now if you write that table, you will get your expected result.

    T = table({x}, mat2str(y));
    writetable(T,'T.txt','Delimiter','\t');
    

    Result:

    Var1    Var2
    x   [1 2 3 4]