From: jrenfree on
Hi all,

I'm running into trouble using sprintf to display data when I have a
cell array of strings. For an example, let's say I have:

a = {'one' 'two' 'three'};
b = [1 2 3];

I'd like my output to say:

one = 1
two = 2
three = 3

but when I try:

sprintf('%s = %d\n', a, b)

It gives me an error about using cell arrays in sprintf. Anything
else I try, such as a{:}, [a{:}], char(a), etc... doesn't work.

Any tips?

Thanks!

-J
From: Jan Simon on
Dear jrenfree!

> a = {'one' 'two' 'three'};
> b = [1 2 3];
>
> I'd like my output to say:
> one = 1
> two = 2
> three = 3

Use a FOR loop.
The CELL approach uses a FOR loop also in NUM2CELL:
ab = cat(1, a, num2cell(b));
sprintf('%s = %d\n', ab{:})

Good luck, Jan
From: fred broccard on
in sprintf(format, A, ...) A is an array. So try with a for loop by indexing your cell array

for ind=1:3
sprintf('%s = %d\n',a{ind},b(ind));
end

for sure there should be a more elegant way though..

jrenfree <jrenfree(a)gmail.com> wrote in message <7d244640-b157-448e-bef5-b4e0b7fc78f0(a)11g2000prv.googlegroups.com>...
> Hi all,
>
> I'm running into trouble using sprintf to display data when I have a
> cell array of strings. For an example, let's say I have:
>
> a = {'one' 'two' 'three'};
> b = [1 2 3];
>
> I'd like my output to say:
>
> one = 1
> two = 2
> three = 3
>
> but when I try:
>
> sprintf('%s = %d\n', a, b)
>
> It gives me an error about using cell arrays in sprintf. Anything
> else I try, such as a{:}, [a{:}], char(a), etc... doesn't work.
>
> Any tips?
>
> Thanks!
>
> -J
From: sscnekro on
>a = {'one' 'two' 'three'};
>b = [1 2 3];

Hi there, is this what you want?
c = arrayfun(@(col) strcat(a{1,col}, ' = ', [blanks(1) num2str(b(1,col))] ), 1:size(b,2),'Unif', false);

PS Walter, us, Jan, dpb, Steve, Bruno, Jos & co.: have you noticed some progress in my programming skills???
From: sscnekro on
A small typo.. for vertical output do
c = arrayfun(@(col) strcat(a{1,col}, ' = ', [blanks(1) num2str(b(1,col))] ), 1:size(b,2),'Unif', false)';