fprintf Formatting Issue - Padding FLOATs with '0'
Mostrar comentarios más antiguos
I want to generate a simple report during the running of a modal analysis program I am generating. However, I'm having a bit of an issue with getting the right format Spec for the fprintf function.
Say I want to lay this report out such that each line (for each mode) has the format:
where all the values of M, md, objectName ARC and DR should line up nicely. Two main reasons for this:
- It's easier to read and spot what I'm looking for.
- I have OCD and can't stand the sight of poorly formatted ASCII files!
But if I run the following, to test the above by outputting to the command window:
%%Create Test Data
M=(1:100)'; % Test Mode Numbers Array
md=ones(size(M)); % Test Max Displacements Array (all ones)
objectName=cell(size(M)); % Test Object Name Cell Array
[objectName{:}]=deal('obj1'); % Test (Say all Same Object 'obj1')
o2i=2:2:length(M); % Make Every 2nd Mode 'obj2'
[objectName{o2i}]=deal('obj2'); % "" "" "" "" ""
ARC=2000*rand(size(M)); % Test (Random) ArcLength Values
D=randi([1,3],size(M)); % Test Direction Array
%%Now Print to Command Window
for i=1:length(M); % FOR EACH MODE...
fprintf(1,['Mode %03d has Max Disp %1.3f on %4s',...
' at ArcLength %04.3f m in Direction %1d\r\n'],...
M(i),md(i),objectName{i},ARC(i),D(i));
end
I get an output like this:
Mode 092 has Max Disp 1.000 on obj2 at ArcLength 1629.080 m in Direction 3
Mode 093 has Max Disp 1.000 on obj1 at ArcLength 1578.147 m in Direction 2
Mode 094 has Max Disp 1.000 on obj2 at ArcLength 1704.528 m in Direction 3
Mode 095 has Max Disp 1.000 on obj1 at ArcLength 1011.273 m in Direction 2
Mode 096 has Max Disp 1.000 on obj2 at ArcLength 1271.323 m in Direction 2
Mode 097 has Max Disp 1.000 on obj1 at ArcLength 1901.789 m in Direction 1
Mode 098 has Max Disp 1.000 on obj2 at ArcLength 887.928 m in Direction 1
Mode 099 has Max Disp 1.000 on obj1 at ArcLength 120.038 m in Direction 1
Mode 100 has Max Disp 1.000 on obj2 at ArcLength 1733.500 m in Direction 3
Note the FUGLINESS of this! It's not a huge problem when all the values of ARC are relatively high (100<=ARC<=9999), but if we had smaller values (10, 51.3, etc) then the txt after the ARC output could jump around a lot.
It seems it's my formatter for the ARC value output that's not working correctly. For some reason, the %04.3f isn't padding out up-to 4 values before the decimal point. It IS padding out the 3 AFTER the decimal point, though.
I've mucked about for a bit trying to find a format string that works but can't seem to find it.
Anyone able to help me out?
To summarise, in the above output, I want ARC values of (for example) 67.39 to appear as 0067.390 in the output.
Any help would be greatly appreciated.
Gav
NB: I'm on Windows 7, 64 bit, MATLAB ver 8.0 (R2012b)
Respuesta aceptada
Más respuestas (0)
Categorías
Más información sobre MATLAB en Centro de ayuda y File Exchange.
Productos
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!