MATLAB: Fprintf cell array of two classes

fprintf cell arrays

Hi all,
I need to use fprintf to print out a char array (strictly 3 columns) and a double array side-by-side so they align. For instance:
charArr = ['USA' ; 'CAN' ; 'FRA']
doubArr = [1 2 3 4; 3 4 5 6; 6 7 8 9]
I want it to look something like:
USA 1 2 3 4
CAN 3 4 5 6
FRA 6 7 8 9
I tried to use cell arrays like below:
table = {charArr, doubArr};
fprintf( '%c, %d', table{:})
But I always get something funky and never what I want. Any help would be appreciated.

Best Answer

Hi,
Use this if the number of rows of both the arrays are same
n = size(doubArr,1);
arrayfun(@(i) fprintf( '%s %s \n', charArr(i,:),num2str(doubArr(i,:))),1:n)
% You can even try this
comB = [charArr + " " + num2str(doubArr)];
fprintf('%s\n', comB)
Hope this helps.
Regards,
Sriram