MATLAB: Fprintf with repeating labels and numbers

cellsfprintflabel;numbersstringstable

I donĀ“t know how to work properly with fprintf and have a problem creating a table with specific labels for my rows. I have the following example code so far:
% Elements
elem_id = 0 ;
res_id = 1 ;
group_id = 0 ;
% Coordinates
x = [1 1 9 ;
1 1 6 ;
1 1 0 ;
2 2 9 ;
2 2 6 ;
2 2 0 ;
];
g = 1 ;
for n = 1:length(x)
fprintf( '%5i %4s %4i %8.3f%8.3f%8.3f\n', elem_id + n, 'A', group_id + g, x(n,1), x(n,2), x(n,3))
if mod(n,3) == 0
g = g + 1 ;
end
end
which produces the following output:
1 A 1 1.000 1.000 9.000
2 A 1 1.000 1.000 6.000
3 A 1 1.000 1.000 0.000
4 A 2 2.000 2.000 9.000
5 A 2 2.000 2.000 6.000
6 A 2 2.000 2.000 0.000
Everything is fine except for the second column, which I want it to be like this:
1 A 1 1.000 1.000 9.000
2 B 1 1.000 1.000 6.000
3 B 1 1.000 1.000 0.000
4 A 2 2.000 2.000 9.000
5 B 2 2.000 2.000 6.000
6 B 2 2.000 2.000 0.000
I fixed it as 'A' for all rows just to keep the code working because everything I have tried is not working so far. Basically I tried something like this:
second_column= {'A' 'B' 'B'}' ;
second_column = repmat(second_column, length(x)/3, 1) ;
and then just call second_column(n) in my fprintf line. However, I get an error saying fprintf is not defined for cell elements, but everywhere I try to look for a similar solution people always use cells with fprintf which got me even more confused!
As you can see my data is grouped in 3s, and what I want is the first element to have a certain label and the second and third element to have a different label. I hope my question is clear and will appreciate any help!

Best Answer

First a warning, never use length on a matrix. If your x has less than 3 rows or if you ever increase the number of columns of x so that it's greater than the number of rows, your code will break. You are asking for the height of the matrix which is size(x, 1) not length(x).
fprintf does not accept cell array inputs so your people always use cells with fprintf which got me even more confused is wrong. Note that there a difference between:
fprintf('%s', SomeCellArray);
and
fprintf('%s', SomeCellArray{:});
The former pass a cell array to fprintf which is an error. The latter pass the content of the cell array as a comma separated list (multiple inputs) to fprintf. This is equivalent to:
fprintf('%s', SomeCellArray{1}, SomeCellArray{2}, ..., SomeCellArray{end}) %except you can't write that in matlab
For your problem, you could just index your second_column in your loop:
second_column = repmat({'A';'B';'B'}, size(x, 1)/3, 1);
for n = 1 : size(x, 1)
fprintf( '%5i %4s %4i %8.3f%8.3f%8.3f\n', elem_id + n, second_column{n}, group_id + g, x(n,1), x(n,2), x(n,3))
%...
end
Alternatively, you could actually build a cell array and fprintf its content which avoids having to use any loop:
print_array = [num2cell(elem_id + (1:size(x, 1))'), ... first column
repmat({'A';'B';'B'}, size(x, 1)/3, 1), ... second column
num2cell(repelem(group_id + (1:size(x, 1)/3)', 3)), ... third column
num2cell(x)]; %4th to 6th column
print_array = print_array.'; %transpose for fprintf
fprintf('%5i %4s %4i %8.3f%8.3f%8.3f\n', print_array{:})