MATLAB Answers

fprintf cell array of two classes

7 views (last 30 days)
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.

  0 Comments

Sign in to comment.

Accepted Answer

Sriram Tadavarty
Sriram Tadavarty on 17 Mar 2020
Edited: Sriram Tadavarty on 17 Mar 2020
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

  2 Comments

Lawrence Wang
Lawrence Wang on 17 Mar 2020
Thanks! Your second expression with the comB part is perfect. If you don't mind could you explain how exactly you got there, I'm trying to understand.
Sriram Tadavarty
Sriram Tadavarty on 17 Mar 2020
Sure Lawrence.
It need to print the combination of numbers and characters. Since, the number of rows in both are same, the general apporach is to concatenate both the variables charArr and doubArr, like [charArr doubArr] but this converts the numeric values to the respective ASCII values. So inorder to make it combined through characters, i converted the numeric value to string value through num2str(doubArr), and then in the output you need additional space between the two arrays and each array needs to print a new line. So, comes the usage of " " in the comB variable and \n operator in fprintf respectively.
Hope this helps.

Sign in to comment.

More Answers (2)

Stephen Cobeldick
Stephen Cobeldick on 17 Mar 2020
Edited: Stephen Cobeldick on 17 Mar 2020
No need for complex strings, ugly loops, or slow arrayfun, you just need one simple fprintf call::
>> A = ['USA' ; 'CAN' ; 'FRA'];
>> B = [1 2 3 4; 3 4 5 6; 6 7 8 9];
>> C = [cellstr(A),num2cell(B)].'; % transpose to get correct order
>> fprintf('%s %d %d %d %d\n', C{:})
USA 1 2 3 4
CAN 3 4 5 6
FRA 6 7 8 9
If you want to learn how to use MATLAB efficiently, you might as well start now.

  3 Comments

Lawrence Wang
Lawrence Wang on 17 Mar 2020
Hi, thanks for you answer. I agree this is much more efficient and it works very well for this instance, however when I try to extend it to other double arrays the output isn't perfectly aligned
For example:
charArr = ['USA' ; 'CAN' ; 'FRA'];
doubArr = [ 1 0 12 3; 9 12 11 21; 2 5 9 2];
C = [cellstr(charArr),num2cell(doubArr)].';
fprintf( '%s %d %d %d %d\n', C{:})
USA 1 0 12 3
CAN 9 12 11 21
FRA 2 5 9 2
Is there any way to rectify this?
Stephen Cobeldick
Stephen Cobeldick on 17 Mar 2020
"Is there any way to rectify this? "
Of course! Just select a suitable fieldwidth, e.g.:
>> A = ['USA' ; 'CAN' ; 'FRA'];
>> B = [1,0,12,3;9,12,11,21;2,5,9,2];
>> C = [cellstr(A),num2cell(B)].';
>> fprintf('%5s %3d %3d %3d %3d\n', C{:})
USA 1 0 12 3
CAN 9 12 11 21
FRA 2 5 9 2
The fprintf documentation explains the fprintf options.
Lawrence Wang
Lawrence Wang on 17 Mar 2020
Oh man I can't believe I forgot that. I'll definitely brush up on the docs. Thanks so much for your help.

Sign in to comment.


Bhaskar R
Bhaskar R on 17 Mar 2020
Edited: Bhaskar R on 17 Mar 2020
require = string(cell2mat(strcat(charArr,{' '}, num2str(doubArr))));

  0 Comments

Sign in to comment.


Translated by