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)?

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

Your sizes are not consistent. What is the output you would look for from this?
The desired result would be a table with the first row consisting in the title array. And below each element of the title array (column) would lay each of the other arrays (a, b and c). In the case of a and b arrays, there are two columns, but if converted into a string it could become just one. I guess if that was the case the sizes would be consistent.
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.
The aim of my final representation of the data in a txt file would be as the case you presented last.
"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."
I am not sure whether I am looking for a formatted output (due to my own ignorance of what that means in the present context). Nevertheless, my aim is to present the data in a way that is visually simple to go through - just like the last case you presented. A useful information would be to know which computational strategies are better to reach that goal. For instance, (1) how to concatenate these different arrays to generate a matrix with the same form as the target table; (2) is it necessary to make data type conversions for the different arrays? Which ones and how?
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.
I think i understand the idea regarding the formatting... But regarding my other questions: (1) how to concatenate these different arrays to generate a matrix with the same form as the target table; (2) is it necessary to make data type conversions for the different arrays? Which ones and how? Do you have any suggestion, or can you show me how you would do it in code?

Sign in to comment.

 Accepted Answer

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

More Answers (3)

The solution is elegant, however my problem is that I am using Matlab version R2011a and therefore the function table (introduced in R2013b) is not available.
(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.
Thanks for the contribute. I will try to use these suggestions in my case.

Categories

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!