I've hit some unexpected behaviour using the fprintf() function in MATLAB. I'm trying to print a multi-line file using the contents of a cell array and a numerical array. I know that I can use the fprintf() function as follows to print out the contents of a cell array:
myCellArray = {'one','two','three'};
fprintf('%s\n',myCellArray{:})
This results in the following output:
one
two
three
I can also print out a numerical array as follows:
myNumericalArray = [1,2,3];
fprintf('%i\n',myNumericalArray)
This results in:
1
2
3
However, the weird behaviour appears if I try to mix these, as follows:
fprintf('%s is %i\n',myCellArray{:},myNumericalArray)
This results in:
one is 116
wo is 116
hree is 1
I think this happens because MATLAB tries to print the next entry in myCellArray in the place of the %i, rather than using the first entry in myNumericalArray. This is evident if I type the following:
fprintf('%s %s\n',myCellArray{:},myCellArray{:})
Which results in:
one two
three one
two three
...Is there some way to ensure that only one element from each array is used per line?