fprintf with repeating labels and numbers
Ältere Kommentare anzeigen
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!
Akzeptierte Antwort
Weitere Antworten (0)
Kategorien
Mehr zu Operators and Elementary Operations finden Sie in Hilfe-Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!