Using fprintf
I want to produce an output that looks like this:
names abc and numbers 1
names def and numbers 2
names ghi and numbers 3
This is the code I tried using to achieve this:
names= {'abc','def','ghi'}
numbers = [1 2 3];
fprintf('names %s and numbers %2.2f \n',names{1:3},numbers)
unfortunatly the output it produces looks like this:
names abc and numbers 100.00
names ef and numbers 103.00
names hi and numbers 1.00
names and number
Does anyone know how to solve this issue? Or is it even possible to combine fprintf
with cell-arrays? Thanks in advance
Take a look at what you are passing to fprintf
, it is simply in the wrong order, and numbers creates one parameter not three individual:
>> names{1:3},numbers
ans =
abc
ans =
def
ans =
ghi
numbers =
1 2 3
Instead use:
C=names
C(2,:)=num2cell(numbers)
fprintf('names %s and numbers %2.2f \n',C{:})
If you typie in C{:}
you will see the individual parameters in order:
>> fprintf('names %s and numbers %2.2f \n',C{:})
names abc and numbers 1.00
names def and numbers 2.00
names ghi and numbers 3.00
>> C{:}
ans =
abc
ans =
1
ans =
def
ans =
2
ans =
ghi
ans =
3