fprintf with repeating labels and numbers

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

Accepted Answer

Guillaume
Guillaume on 12 Sep 2018
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{:})
  2 Comments
Eugenio Gil
Eugenio Gil on 12 Sep 2018
Thank you for the clarification! I was trying your first suggestion but with second_column(n) instead of with curly brackets, so I assume that passes a cell type as well (first time working with cells)... then I tried the second way but again, I guess without a clear understanding of cells I couldn´t figure out how to make it work! Very helpful to see both ways.
Stephen23
Stephen23 on 12 Sep 2018
@Eugenio Gil: cell array indexing in a nutshell:
  • {} curly braces access the cell contents.
  • () parentheses access the cells themselves.
You might also be interested to read this, in addition to the link Guillaume gave:

Sign in to comment.

More Answers (0)

Categories

Find more on Numeric Types in Help Center and File Exchange

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!