fprintf Formatting Issue - Padding FLOATs with '0'

25 Ansichten (letzte 30 Tage)
Gavin
Gavin am 4 Jul. 2013
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:
" Mode M has Max Disp md on objectName at ArcLength ARC m in Direction DR"
where all the values of M, md, objectName ARC and DR should line up nicely. Two main reasons for this:
  1. It's easier to read and spot what I'm looking for.
  2. 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)

Akzeptierte Antwort

dpb
dpb am 4 Jul. 2013
fprintf(1,['Mode %03d ...
' at ArcLength %04.3f m in Direction %1d\r\n'],...
I get an output like this:
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
in the above output, I want ARC values of (for example) 67.39 to appear as 0067.390 in the output.
Indeed.
The field width 4.3f isn't wide enough to contain the data so there's no room for zero padding. Unfortunately, unlike Fortran, C doesn't warn on malformed formats but just blithely does whatever it chooses.
The field width required for '0067.390' is 8--use '%08f.3' and joy should ensue...
  2 Kommentare
Gavin
Gavin am 4 Jul. 2013
Ah okay. So the number before the ' . ' is actually the total field width, and not the number of characters before the ' . '? This is a HUGE fundamental misunderstanding on my part!! I'm surprised it hasn't caught me out before!! Thanks for your help.
Gav
dpb
dpb am 4 Jul. 2013
Bearbeitet: dpb am 4 Jul. 2013
doc sprintf % and/or friends
read the format description section
That you haven't been "caught out" before is probably owing to the propensity of C i/o runtime to just do its own thing despite such malformed specifications as you observed above. It didn't do what you wanted but didn't give any indication of a problem, either. On this particular error, Fortran would fill the field w/ asterisks ('*') which tells you the value doesn't fit the field.

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (0)

Kategorien

Mehr zu MATLAB finden Sie in Help Center und File Exchange

Produkte

Community Treasure Hunt

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

Start Hunting!

Translated by