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)?
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
Your sizes are not consistent. What is the output you would look for from this?
Rui Azevedo
on 17 Jan 2016
Edited: Rui Azevedo
on 17 Jan 2016
Walter Roberson
on 17 Jan 2016
Your desired result would be
{'a', 'b', 'c';
'01', 'noyes', [];
'01', 'noyes', 1;
'01', 'noyes', '12'}
or
{'a', 'b', 'c';
'0 1', 'no yes', [];
'0 1', 'no yes', 1;
'0 1', 'no yes', '1 2'}
or
{'a', 'b', 'c';
'0,1', 'no,yes', [];
'0,1', 'no,yes', 1;
'0,1', 'no,yes', '1,2'}
Or are you aiming for a final representation as a space delimited file with text that is not intended to be interpreted by computers and which would look like
a b c
0 1 no yes
0 1 no yes 1
0 1 no yes 1 2
with each title centered over the maximum of the content that is below it?
If you are looking for formatted output, 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.
The techniques you would use for formatted text output would not be the same as what you would use if you were creating cell arrays for writing to excel, or if you were creating csv files, or if you were producing an HTML table.
Rui Azevedo
on 17 Jan 2016
Edited: Rui Azevedo
on 17 Jan 2016
Walter Roberson
on 17 Jan 2016
"If you are looking for formatted output, 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."
Rui Azevedo
on 17 Jan 2016
Edited: Rui Azevedo
on 17 Jan 2016
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.
Rui Azevedo
on 17 Jan 2016
Accepted Answer
More Answers (3)
Rui Azevedo
on 17 Jan 2016
0 votes
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.
Rui Azevedo
on 18 Jan 2016
0 votes
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!