What is the best way to concatenate arrays of different types (matrices and cells of strings or numbers) into a table to be saved in txt file (for Matlab R2011a)?
3 views (last 30 days)
Show older comments
Example:
title = ['a', 'b', 'c'];
N = 3;
a = [zeros(N,1), ones(N,1)];
b = {repmat('no',N,1), repmat('yes',N,1)};
c = {[]; 1; [1,2]}
table = {title; a, b, c}
8 Comments
Walter Roberson
on 17 Jan 2016
Edited: Walter Roberson
on 17 Jan 2016
If you are looking for an output like the last one I presented, you need to give the rules for how many digits to print if the values are not integers, and rules about when to use exponential notation or fixed-point notation, and you need to give the rules for whether values for columns are to be left-aligned or right-aligned or center-aligned.
Oh yes, and if there are rules about the maximum number of values that can be put into one column before the values have to be split into multiple lines, then you need to talk about that too.
Accepted Answer
Peter Perkins
on 17 Jan 2016
Under simple conditions, writetable just does what you want:
>> N = 3;
>> a = [zeros(N,1) ones(N,1)];
>> b = [repmat({'no'},N,1) repmat({'yes'},N,1)]; % slightly different than yours
>> c = {[]; 1; [1,2]};
>> t = table(a, b, c)
t =
a b c
______ _____________ ____________
0 1 'no' 'yes' []
0 1 'no' 'yes' [ 1]
0 1 'no' 'yes' [1x2 double]
>> writetable(t,'t.dat')
>> type t.dat
a_1,a_2,b_1,b_2,c_1,c_2
0,1,no,yes,,
0,1,no,yes,1,
0,1,no,yes,1,2
0 Comments
More Answers (3)
Walter Roberson
on 17 Jan 2016
(1) how to concatenate these different arrays to generate a matrix with the same form as the target table;
Answer: convert each of the arrays to a cell array which has one entry for each row and column. As you want potentially multiple values at each location, the numeric entries with multiple values would be row vectors of values, and the string entries with multiple values would be cell arrays of strings.
(2) is it necessary to make data type conversions for the different arrays? Which ones and how?
You only need to convert to cell array. If you have a 2D numeric array like your "a" that is to be converted to one cell per row, then use
mat2cell(Your2DArray, ones(1, size(Your2DArray,1)), size(Your2DArray,2))
Your "c" is already a cell array in the proper form and does not need to be converted.
Your "b" is already a cell array in the proper form for one continuous string per row and does not need to be converted. However, the discussion suggests strongly that you created your "b" incorrectly and need the code Peter showed,
b = [repmat({'no'},N,1) repmat({'yes'},N,1)];
This would not need to be converted.
Your title = ['a', 'b', 'c'] is equivalent to title = 'abc' so you will need to fix that, title = {'a', 'b', 'c'} . Once done it would not need to be converted.
This shows how to put together a cell array with the appropriate contents. You have indicated that you can handle the formatting from there.
If your desired output is as I asked about in the Comments above, then I would find it a lot easier to format the content to strings as I went along, and then later examine by column to work out the widths and centering needed for the strings, but formatting a mix of numeric and string items is certainly possible.
0 Comments
See Also
Categories
Find more on Matrices and Arrays in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!