Thread Subject:
fprintf not correctly aligning numbers and text

Subject: fprintf not correctly aligning numbers and text

From: Eli

Date: 5 Jul, 2012 17:22:07

Message: 1 of 20

I have printed some data to a text file using:

fmt3=[repmat(' %6.2f',1,((d-1)/2)) '\n']; %format for fprintf to print data
ext2='.txt'; %extension in which files will be saved
filename2=[name2,num2str(z),ext2]; %construct the file name for saving

fid3=fopen(filename2,'wt'); %open file for printing data
fprintf(fid3,fmt3,ROI_matrix'); %print data
fclose(fid3); %close file to which data was printed

This is working and I am seeing the data in columns, properly organized. Here is an example (d = 21):

    1.00 5477.73 94.17 25.07 30.65 74.33 32.03 328.91 143.73 24.02 97.61
    2.00 5509.56 120.24 24.05 22.15 74.76 25.39 468.64 102.85 65.83 126.30
    3.00 5458.51 162.50 26.68 29.34 78.91 25.92 266.72 105.32 15.09 90.37
    4.00 5416.76 137.33 23.97 19.06 80.81 51.96 129.41 62.69 46.09 69.06

However, I would now like to label columns 2-11 [that's 10 columns, where the number 10 is determined by ((d-1)/2)-1] with column headings. I'm having real trouble getting fprintf to work correctly for this purpose. There are 2 conditions:
1. The column headings come from a cell structure. So I must use the following:

fmt4=[repmat('%6s',1,((d-1)/2)) '\n']; %format for fprintf to print column headings

for ii=3:2:(d-2)
fprintf(fid3,fmt4,(char(arr.textdata(ii))));
end

This works. It extracts the required information and converts it into characters. The right number of column headings are being extracted for each file that is being processed (I have verified this with 2 files, each having a different number of columns). However, when I fprintf(), here is what I am getting:

  Ar-K Ca-K Cr-K Mn-K Fe-K Ni-K Cu-K Zn-K As-Ka Ta-L 1.00 5477.73 94.17 25.07 30.65 74.33 32.03 328.91 143.73 24.02 97.61
    2.00 5509.56 120.24 24.05 22.15 74.76 25.39 468.64 102.85 65.83 126.30
    3.00 5458.51 162.50 26.68 29.34 78.91 25.92 266.72 105.32 15.09 90.37
    4.00 5416.76 137.33 23.97 19.06 80.81 51.96 129.41 62.69 46.09 69.06

a. The column headings are correct in number. But they are not correctly lined up (they are not starting at column 2, as I require, but instead at column 1).
b. After the first row of headings, the numeric data are not starting on the next line (line 2). Instead, the first row of data is beginning right after the column headings end, on the first line itself.

2. The first column must either have the heading "index" or be left blank. I am not concerned about the heading for this column, so either choice will do.

Is there some way for me to fix the fprintf() command to get it to correctly (in terms of alignment) print out the headings?

Subject: fprintf not correctly aligning numbers and text

From: Eli

Date: 5 Jul, 2012 17:34:07

Message: 2 of 20

Sorry, d = 23 above.

Subject: fprintf not correctly aligning numbers and text

From: KJ

Date: 5 Jul, 2012 17:35:31

Message: 3 of 20

first of all i think you want to check if all your fprintf %6d are used.
Since your d=23, that means you should have 11 columns, and you only have 10 headers. That is the cause of such problem.

On Thursday, July 5, 2012 1:22:07 PM UTC-4, Eli wrote:
> I have printed some data to a text file using:
>
> fmt3=[repmat(' %6.2f',1,((d-1)/2)) '\n']; %format for fprintf to print data
> ext2='.txt'; %extension in which files will be saved
> filename2=[name2,num2str(z),ext2]; %construct the file name for saving
>
> fid3=fopen(filename2,'wt'); %open file for printing data
> fprintf(fid3,fmt3,ROI_matrix'); %print data
> fclose(fid3); %close file to which data was printed
>
> This is working and I am seeing the data in columns, properly organized. Here is an example (d = 21):
>
> 1.00 5477.73 94.17 25.07 30.65 74.33 32.03 328.91 143.73 24.02 97.61
> 2.00 5509.56 120.24 24.05 22.15 74.76 25.39 468.64 102.85 65.83 126.30
> 3.00 5458.51 162.50 26.68 29.34 78.91 25.92 266.72 105.32 15.09 90.37
> 4.00 5416.76 137.33 23.97 19.06 80.81 51.96 129.41 62.69 46.09 69.06
>
> However, I would now like to label columns 2-11 [that's 10 columns, where the number 10 is determined by ((d-1)/2)-1] with column headings. I'm having real trouble getting fprintf to work correctly for this purpose. There are 2 conditions:
> 1. The column headings come from a cell structure. So I must use the following:
>
> fmt4=[repmat('%6s',1,((d-1)/2)) '\n']; %format for fprintf to print column headings
>
> for ii=3:2:(d-2)
> fprintf(fid3,fmt4,(char(arr.textdata(ii))));
> end
>
> This works. It extracts the required information and converts it into characters. The right number of column headings are being extracted for each file that is being processed (I have verified this with 2 files, each having a different number of columns). However, when I fprintf(), here is what I am getting:
>
> Ar-K Ca-K Cr-K Mn-K Fe-K Ni-K Cu-K Zn-K As-Ka Ta-L 1.00 5477.73 94.17 25.07 30.65 74.33 32.03 328.91 143.73 24.02 97.61
> 2.00 5509.56 120.24 24.05 22.15 74.76 25.39 468.64 102.85 65.83 126.30
> 3.00 5458.51 162.50 26.68 29.34 78.91 25.92 266.72 105.32 15.09 90.37
> 4.00 5416.76 137.33 23.97 19.06 80.81 51.96 129.41 62.69 46.09 69.06
>
> a. The column headings are correct in number. But they are not correctly lined up (they are not starting at column 2, as I require, but instead at column 1).
> b. After the first row of headings, the numeric data are not starting on the next line (line 2). Instead, the first row of data is beginning right after the column headings end, on the first line itself.
>
> 2. The first column must either have the heading "index" or be left blank. I am not concerned about the heading for this column, so either choice will do.
>
> Is there some way for me to fix the fprintf() command to get it to correctly (in terms of alignment) print out the headings?



On Thursday, July 5, 2012 1:22:07 PM UTC-4, Eli wrote:
> I have printed some data to a text file using:
>
> fmt3=[repmat(' %6.2f',1,((d-1)/2)) '\n']; %format for fprintf to print data
> ext2='.txt'; %extension in which files will be saved
> filename2=[name2,num2str(z),ext2]; %construct the file name for saving
>
> fid3=fopen(filename2,'wt'); %open file for printing data
> fprintf(fid3,fmt3,ROI_matrix'); %print data
> fclose(fid3); %close file to which data was printed
>
> This is working and I am seeing the data in columns, properly organized. Here is an example (d = 21):
>
> 1.00 5477.73 94.17 25.07 30.65 74.33 32.03 328.91 143.73 24.02 97.61
> 2.00 5509.56 120.24 24.05 22.15 74.76 25.39 468.64 102.85 65.83 126.30
> 3.00 5458.51 162.50 26.68 29.34 78.91 25.92 266.72 105.32 15.09 90.37
> 4.00 5416.76 137.33 23.97 19.06 80.81 51.96 129.41 62.69 46.09 69.06
>
> However, I would now like to label columns 2-11 [that's 10 columns, where the number 10 is determined by ((d-1)/2)-1] with column headings. I'm having real trouble getting fprintf to work correctly for this purpose. There are 2 conditions:
> 1. The column headings come from a cell structure. So I must use the following:
>
> fmt4=[repmat('%6s',1,((d-1)/2)) '\n']; %format for fprintf to print column headings
>
> for ii=3:2:(d-2)
> fprintf(fid3,fmt4,(char(arr.textdata(ii))));
> end
>
> This works. It extracts the required information and converts it into characters. The right number of column headings are being extracted for each file that is being processed (I have verified this with 2 files, each having a different number of columns). However, when I fprintf(), here is what I am getting:
>
> Ar-K Ca-K Cr-K Mn-K Fe-K Ni-K Cu-K Zn-K As-Ka Ta-L 1.00 5477.73 94.17 25.07 30.65 74.33 32.03 328.91 143.73 24.02 97.61
> 2.00 5509.56 120.24 24.05 22.15 74.76 25.39 468.64 102.85 65.83 126.30
> 3.00 5458.51 162.50 26.68 29.34 78.91 25.92 266.72 105.32 15.09 90.37
> 4.00 5416.76 137.33 23.97 19.06 80.81 51.96 129.41 62.69 46.09 69.06
>
> a. The column headings are correct in number. But they are not correctly lined up (they are not starting at column 2, as I require, but instead at column 1).
> b. After the first row of headings, the numeric data are not starting on the next line (line 2). Instead, the first row of data is beginning right after the column headings end, on the first line itself.
>
> 2. The first column must either have the heading "index" or be left blank. I am not concerned about the heading for this column, so either choice will do.
>
> Is there some way for me to fix the fprintf() command to get it to correctly (in terms of alignment) print out the headings?

Subject: fprintf not correctly aligning numbers and text

From: Eli

Date: 5 Jul, 2012 17:49:10

Message: 4 of 20

Yes, that is true. The reason for the difference between the number of headers (10) and the number of columns (11) is that first header. Like I said in condition 2.) either that first header must be blank or it must have the header 'index'. The problem is that my other 10 headers come from a cell structure: arr.textdata. Also, I had to come up with a loop to extract this information.

I am not sure though how to add 'index' to this cell structure. If this could be done and I would have 11 headers and 11 columns, then I could change my fprinf() command to print 11 column headers instead of 10.

This seems like it would be quite tricky.

Isn't there a way to tell fprintf to print one row (headers) that is horizontally offset from the other rows (numeric)? To me, this seems like a much easier solution.

KJ <likejie@gmail.com> wrote in message <f12d2991-7955-4c0a-a5dd-6be85f7dda95@googlegroups.com>...
> first of all i think you want to check if all your fprintf %6d are used.
> Since your d=23, that means you should have 11 columns, and you only have 10 headers. That is the cause of such problem.

Subject: fprintf not correctly aligning numbers and text

From: KJ

Date: 5 Jul, 2012 17:52:18

Message: 5 of 20

yeah for your header you have one fprintf just print the header.
and for other rows you can have another fprintf to print them out

On Thursday, July 5, 2012 1:49:10 PM UTC-4, Eli wrote:
> Yes, that is true. The reason for the difference between the number of headers (10) and the number of columns (11) is that first header. Like I said in condition 2.) either that first header must be blank or it must have the header 'index'. The problem is that my other 10 headers come from a cell structure: arr.textdata. Also, I had to come up with a loop to extract this information.
>
> I am not sure though how to add 'index' to this cell structure. If this could be done and I would have 11 headers and 11 columns, then I could change my fprinf() command to print 11 column headers instead of 10.
>
> This seems like it would be quite tricky.
>
> Isn't there a way to tell fprintf to print one row (headers) that is horizontally offset from the other rows (numeric)? To me, this seems like a much easier solution.

Subject: fprintf not correctly aligning numbers and text

From: dpb

Date: 5 Jul, 2012 19:08:35

Message: 6 of 20

On 7/5/2012 12:22 PM, Eli wrote:
...
> 2. The first column must either have the heading "index" or be left
> blank. I am not concerned about the heading for this column, so either
> choice will do.
>
> Is there some way for me to fix the fprintf() command to get it to
> correctly (in terms of alignment) print out the headings?

Of course... :)

For the first issue simply add the first fixed column label into the
format string...make sure the field count matches the number of column
labels to force the newline to happen

fmt4=[' Index' repmat('%6s',1,((d-1)/2)) '\n'];

A fixed number of blanks in place of ' Index' will do the same spacing.

Your solution for extracting/printing the labels from a cell array is
the way it must be done unless you want to first scan to a string
variable and then do the external io of that one string. For small
amounts of data it won't matter.

Unfortunately, what looks like it should work, that is, something like

fprintf('%6s',char(a.text)')

doesn't bring joy as the width field doesn't force a fixed-width field
and a formatting string of ' %s' that looks like it _should_ force a
character in front of the string doesn't do that (at least in my
release; you might try it there. That is, see what

fprintf(' %6s',char(a.text)')

does just for grins (I don't think it'll work satisfactorily, but what
the hay... just maybe TMW has done it by now--I see that textscan() now
does handle multiple %s fields so who know? :) )

What's so annoying is that the above _will_ place a ' ' in front of the
beginning string, but ignore it afterwards despite the field width
specifier. It's time I here again lament the lack of a vectorized
Fortran FORMAT syntax in Matlab to make this all essentially trivial. :)

--

Subject: fprintf not correctly aligning numbers and text

From: TideMan

Date: 5 Jul, 2012 21:46:39

Message: 7 of 20

On Friday, July 6, 2012 5:22:07 AM UTC+12, Eli wrote:
> I have printed some data to a text file using:
>
> fmt3=[repmat(' %6.2f',1,((d-1)/2)) '\n']; %format for fprintf to print data
> ext2='.txt'; %extension in which files will be saved
> filename2=[name2,num2str(z),ext2]; %construct the file name for saving
>
> fid3=fopen(filename2,'wt'); %open file for printing data
> fprintf(fid3,fmt3,ROI_matrix'); %print data
> fclose(fid3); %close file to which data was printed
>
> This is working and I am seeing the data in columns, properly organized. Here is an example (d = 21):
>
> 1.00 5477.73 94.17 25.07 30.65 74.33 32.03 328.91 143.73 24.02 97.61
> 2.00 5509.56 120.24 24.05 22.15 74.76 25.39 468.64 102.85 65.83 126.30
> 3.00 5458.51 162.50 26.68 29.34 78.91 25.92 266.72 105.32 15.09 90.37
> 4.00 5416.76 137.33 23.97 19.06 80.81 51.96 129.41 62.69 46.09 69.06
>
> However, I would now like to label columns 2-11 [that's 10 columns, where the number 10 is determined by ((d-1)/2)-1] with column headings. I'm having real trouble getting fprintf to work correctly for this purpose. There are 2 conditions:
> 1. The column headings come from a cell structure. So I must use the following:
>
> fmt4=[repmat('%6s',1,((d-1)/2)) '\n']; %format for fprintf to print column headings
>
> for ii=3:2:(d-2)
> fprintf(fid3,fmt4,(char(arr.textdata(ii))));
> end
>
> This works. It extracts the required information and converts it into characters. The right number of column headings are being extracted for each file that is being processed (I have verified this with 2 files, each having a different number of columns). However, when I fprintf(), here is what I am getting:
>
> Ar-K Ca-K Cr-K Mn-K Fe-K Ni-K Cu-K Zn-K As-Ka Ta-L 1.00 5477.73 94.17 25.07 30.65 74.33 32.03 328.91 143.73 24.02 97.61
> 2.00 5509.56 120.24 24.05 22.15 74.76 25.39 468.64 102.85 65.83 126.30
> 3.00 5458.51 162.50 26.68 29.34 78.91 25.92 266.72 105.32 15.09 90.37
> 4.00 5416.76 137.33 23.97 19.06 80.81 51.96 129.41 62.69 46.09 69.06
>
> a. The column headings are correct in number. But they are not correctly lined up (they are not starting at column 2, as I require, but instead at column 1).
> b. After the first row of headings, the numeric data are not starting on the next line (line 2). Instead, the first row of data is beginning right after the column headings end, on the first line itself.
>
> 2. The first column must either have the heading "index" or be left blank. I am not concerned about the heading for this column, so either choice will do.
>
> Is there some way for me to fix the fprintf() command to get it to correctly (in terms of alignment) print out the headings?

Rather than fight Matlab on this matter, why not change the space delimiter to tab,i.e., \t? That will line things up nicely.
Unfortunately, C, C++, and therefore Matlab are bloody hopeless at spacing things nicely for output. Unlike Fortran, which behaves beautifully for such things.

Subject: fprintf not correctly aligning numbers and text

From: Eli

Date: 7 Jul, 2012 20:46:14

Message: 8 of 20

I'm just not getting the headers to line up with the columns. Also the problem I was facing with the first row of data remains.i.e. the first row of data starts on the same line as the headers (just as I showed above).

This is really frustrating. Here is the code:

-------------------You can SKIP this part-------------------
filess = dir('*.*.dat');
q=2; %selecting the second .dat file
[pathstr2,name,ext] = fileparts(filess(q).name); %obtain file name
arr=importdata(filess(q).name); %import the data
data_m=arr.data; %assign data to matrix
[c,d]=size(data_m); %size of matrix is 976 X 23
z = 1;
 
arr.textdata(1)=[]; %remove the first header from the .dat file to make this a 10 X 1 cell

j=1;
for i=3:2:(d-2)
A(:,j)=data_m(:,i); %my command to extract the useful data from the matrix
j=j+1;
end
------------------------------------------------------------

A = rand(976,23); %you can use this to generate the data that I am using
 
A = [(1:c)' A]; %adding the index - a series of numbers from 1 to 976
abc=['Index' arr.textdata]; %add Index to the header cell (arr.textdata was a 10 X 1 cell with the headers, this makes it a 11 X 1 cell)
name2='myfile'; %output file name w/o extension
 
fmt3=[repmat(' %6.2f',1,((d-1)/2)) '\n']; %format to print data
ext2='.txt';
filename2=[name2,num2str(z),ext2]; %construct output filename
       
fid3=fopen(filename2,'wt'); %open file for printing
fmt4=[repmat('%8s',1,((d-1)/2)) '\n']; %format for printing headers
    
for ii=1:2:(d-2)
%fprintf(fid3,fmt4,(char(abc(ii)))); %print headers - 11 headers (cols 1 to 21 in steps of 2)
fprintf(fid3,' %6s',(char(arr.textdata))); %print headers - 11 headers (cols 1 to 21 in steps of 2)
end
 
fprintf(fid3,fmt3,A'); %print matrix
fclose(fid3); %close file to which information was printed

The headers (for this particular file only) are shown in the original post. Their number changes from file to file, which is why I have to use (d-1)/2.

Is the spacing causing me problems? I mean, have I used it incorrectly? Or is there something else that I am messing up?

Subject: fprintf not correctly aligning numbers and text

From: dpb

Date: 7 Jul, 2012 21:04:48

Message: 9 of 20

On 7/7/2012 3:46 PM, Eli wrote:
> I'm just not getting the headers to line up with the columns. Also the
> problem I was facing with the first row of data remains.i.e. the first
> row of data starts on the same line as the headers (just as I showed
> above).
>
> This is really frustrating. Here is the code:
...

I posted a solution earlier that should solve your problem. Look at it
and if there's still something missing repost in reply to it and I'll
look again.

--

Subject: fprintf not correctly aligning numbers and text

From: dpb

Date: 7 Jul, 2012 23:49:06

Message: 10 of 20

On 7/7/2012 3:46 PM, Eli wrote:
> I'm just not getting the headers to line up with the columns. Also the
> problem I was facing with the first row of data remains.i.e. the first
> row of data starts on the same line as the headers (just as I showed
> above).
...

Here's a sample of a file formatted so the headers line up w/ the data
columns...salt to suit.

The key problem in your examples I see is twofold as I noted earlier--

First, the linefeed problem is that the count of fields in the format
string was/is greater than the number of values output for the line.
So, since fprintf() associates each data value w/ a format element, it
exhausted the output before reaching the \n so it was never used (and in
C/Matlab, that's not an error required to be diagnosed). The next
fprintf() uses a different format string which didn't begin w/ a \n so
since C/Matlab is stream io, the output runs on the same record until a
\n is finally run across.

To fix this problem make sure the fmt string count _exactly_ matches the
number of elements to go on the record.

Secondly, your format string for the floats includes at least one extra
space if not two--those are echo'ed in the output string so that the
actual spacing of the floating point values is as if you used %7.2f or
%8.2f instead of a field width of 6. Fix that to eliminate the extra
spaces.

With those corrections, it's pretty straightforward--

 >> A=[(1:10)' rand(10, 6)]; % Don't need anything but a small sample
 >> hdr=cellstr(strvcat('Ar-K','Ca-K','Cr-K','Mn-K','Fe-K','Ni-K'));
 >> fid=fopen('junk.dat','wt');
 >> fprintf(fid,' Index');
 >> for i=1:6
fprintf(fid,'%6s',char(hdr(i)));
end
 >> fprintf(fid,'\n');
 >> fprintf(fid,fmt2,A');
 >> fid=fclose(fid)
 >> type junk.dat

  Index Ar-K Ca-K Cr-K Mn-K Fe-K Ni-K
   1.00 0.95 0.62 0.06 0.02 0.84 0.19
   2.00 0.23 0.79 0.35 0.75 0.02 0.68
   3.00 0.61 0.92 0.81 0.45 0.68 0.30
   4.00 0.49 0.74 0.01 0.93 0.38 0.54
   5.00 0.89 0.18 0.14 0.47 0.83 0.15
   6.00 0.76 0.41 0.20 0.42 0.50 0.70
   7.00 0.46 0.94 0.20 0.85 0.71 0.38
   8.00 0.02 0.92 0.60 0.53 0.43 0.86
   9.00 0.82 0.41 0.27 0.20 0.30 0.85
  10.00 0.44 0.89 0.20 0.67 0.19 0.59

 >>

Salt to suit regarding the selection of different numbers and particular
columns, etc., etc., etc., ... Get the format strings right and all
else will work out ok.

--

Subject: fprintf not correctly aligning numbers and text

From: dpb

Date: 8 Jul, 2012 01:03:15

Message: 11 of 20

On 7/7/2012 6:49 PM, dpb wrote:
...

> Here's a sample of a file formatted so the headers line up w/ the data
> columns...salt to suit.
...

> Secondly, your format string for the floats includes at least one extra
> space if not two--those are echo'ed in the output string so that the
> actual spacing of the floating point values is as if you used %7.2f or
> %8.2f instead of a field width of 6. Fix that to eliminate the extra
> spaces.

I see I didn't get fmt2 pasted... in...since it was one of your specific
problems, while you almost had it, here's the one for the sample...

 >> fmt2=[repmat('%6.2f',1,size(A,2)) '\n']
fmt2 =
%6.2f%6.2f%6.2f%6.2f%6.2f%6.2f%6.2f\n
 >>

NB there are _no_ embedded blanks.


...[previous sample elided for brevity]...

--

Subject: fprintf not correctly aligning numbers and text

From: Eli

Date: 9 Jul, 2012 17:17:14

Message: 12 of 20

Ok, it finally seems to be aligning things correctly.

One small problem though. Here is the output:

  1.005477.73 94.17 25.07 30.65 74.33 32.03328.91143.73 24.02 97.61
  2.005509.56120.24 24.05 22.15 74.76 25.39468.64102.85 65.83126.30
  3.005458.51162.50 26.68 29.34 78.91 25.92266.72105.32 15.09 90.37
  4.005416.76137.33 23.97 19.06 80.81 51.96129.41 62.69 46.09 69.06
  5.005574.42122.20 23.12 26.66 51.16 43.65 89.15 61.67 32.47 65.08
  6.005402.18100.23 24.35 27.30 74.72 41.31 62.62 56.89 26.76 72.02

The columns seem very tightly packed. Just changing:

'%6.2f'

to

'%7.2f'

completely throws off the alignment of the columns.

Is there a way to space them out (perhaps tab spacing) while preserving the column alignment?

Subject: fprintf not correctly aligning numbers and text

From: dpb

Date: 9 Jul, 2012 18:13:58

Message: 13 of 20

On 7/9/2012 12:17 PM, Eli wrote:
> Ok, it finally seems to be aligning things correctly.
>
> One small problem though. Here is the output:
>
> 1.005477.73 94.17 25.07 30.65 74.33 32.03328.91143.73 24.02 97.61
> 2.005509.56120.24 24.05 22.15 74.76 25.39468.64102.85 65.83126.30
...
>
> The columns seem very tightly packed. Just changing:
>
> '%6.2f'
>
> to
> '%7.2f'
>
> completely throws off the alignment of the columns.

Well, your data is too wide for a field width of 6.

And, _of_course_ changing just the numeric field width throws the
alignment off-that's one of the things that was wrong w/ your previous
cases of having the blank in the format string.
>
> Is there a way to space them out (perhaps tab spacing) while preserving
> the column alignment?

Well, a tab will be dependent on the tab spacing in the displaying
medium and in some file formats won't be recognizable so unless you're
really, really making a tab-delimited file for that purpose and not
formatting output for visual consumption then unless you can control the
display medium that may not bring joy, either (then again, if you do
have that control, it might; only you can answer that by trying it in
whatever display that is).

Of course there's a way to space them out--use the same width for the
header fields as for the numeric ones and keep them consistent with each
other.

NB where I had hardcoded six characters in for the ' Index' column
header in my example you'll need to use '%7s' for its format string, too.

As I've pointed out repeatedly, the key is that all fields have to be
the desired (and same) width as the headers and you must ensure you have
the right count of fields per record to force the \n.

 >> fmt2=[repmat('%7.2f',1,size(A,2)) '\n']
 >> fid=fopen('junk.dat','wt');
 >> fprintf(fid,'%7s',' Index');
 >> for i=1:6, fprintf(fid,'%7s', char(hdr(i))); end
 >> fprintf(fid,'\n');
 >> fprintf(fid,fmt2,A');
 >> fid=fclose(fid)
 >> type junk.dat

   Index Ar-K Ca-K Cr-K Mn-K Fe-K Ni-K
    1.00 0.64 0.05 0.50 0.93 0.58 0.61
    2.00 0.21 0.42 0.21 0.68 0.45 0.61
...
    8.00 0.79 0.99 0.74 0.61 0.68 0.37
    9.00 0.06 0.79 0.27 0.63 0.09 0.63
   10.00 0.60 0.44 0.44 0.37 0.04 0.72

 >>

--

Subject: fprintf not correctly aligning numbers and text

From: Eli

Date: 9 Jul, 2012 22:42:27

Message: 14 of 20

> Of course there's a way to space them out--use the same width for the
> header fields as for the numeric ones and keep them consistent with each
> other.
>
> As I've pointed out repeatedly, the key is that all fields have to be
> the desired (and same) width as the headers and you must ensure you have
> the right count of fields per record to force the \n.
>

Ah, I see. It finally worked. The point about the same field width for the headers and the columns themselves was quite important. To be on the safer size, it went with:

'%15.2f'

and

'%15s'

and this for the 'Index' string (field width of 15, I guess, is what it works out to):
' Index'

I just couldn't think of any other way to get the Index header to line up with the end of the columns (which had a field width of 15).

Now, I don't quite see what you meant by the right count of fields per record to force the \n. Could you explain this a bit more?

Subject: fprintf not correctly aligning numbers and text

From: dpb

Date: 10 Jul, 2012 00:19:16

Message: 15 of 20

On 7/9/2012 5:42 PM, Eli wrote:
...

> Ah, I see. It finally worked. The point about the same field width for
> the headers and the columns themselves was quite important. ...

I don't see how you could ever think anything different if the point is
to line them up w/ each other...

...

>
> Now, I don't quite see what you meant by the right count of fields per
> record to force the \n. Could you explain this a bit more?

I don't know any more to say than the explanation I wrote pointing out
the problems in your previous solutions that is the first response to
you in this subthread of the main thread...the one from about 7PM last
evening...

--

Subject: fprintf not correctly aligning numbers and text

From: dpb

Date: 10 Jul, 2012 18:19:17

Message: 16 of 20

On 7/9/2012 7:19 PM, dpb wrote:
> On 7/9/2012 5:42 PM, Eli wrote:
> ....
>
>> Ah, I see. It finally worked. The point about the same field width for
>> the headers and the columns themselves was quite important. ...
>
> I don't see how you could ever think anything different if the point is
> to line them up w/ each other...
>
> ....

BTW, since the first column is an index value it would be
cleaner-looking if it weren't also in floating point format...easy
enough if just keep to the general principles already outlined.

 >> fmt2=['%5d ' repmat('%7.2f',1,size(A,2)-1) '\n'];
 >> fprintf('%7s','Index');
for i=1:length(hdr)
   fprintf('%7s',char(hdr(i)));end; fprintf('\n');
fprintf(fmt2,A');

   Index Ar-K Ca-K Cr-K Mn-K Fe-K Ni-K
     1 0.64 0.05 0.50 0.93 0.58 0.61
     2 0.21 0.42 0.21 0.68 0.45 0.61
...
     9 0.06 0.79 0.27 0.63 0.09 0.63
    10 0.60 0.44 0.44 0.37 0.04 0.72
 >>

--

Subject: fprintf not correctly aligning numbers and text

From: Eli

Date: 13 Jul, 2012 00:39:44

Message: 17 of 20

Ok, that worked. Thanks for the suggestion regarding the index. It is certainly a lot cleaner.

I have a related question and I think it is worth posting it here as it covers fprintf().

Here is how I am printing out 5 headers to a file:

fid1 = fopen(...);
fprintf(fid1,' Headre\n'); %print header 1
fprintf(fid1,' Hede-r\n'); %print header 2
fprintf(fid1,' Hea-dr\n'); %print header 3
fprintf(fid1,' Header\n'); %print header 4
fprintf(fid1,' Hadere\n'); %print header 5
fclose(fid1);

In other words, I am doing it manually. However, I have a text file with these column headers defined in it (each header will always be exactly 6 characters long). Rather than manually entering these headers into 5 different fprintf() commands (as I have done above), I need to load them in and set up a loop to fprintf() them. The file with the headers is called textf.txt and is contents are quite simple. Here are some representative contents:

Headre
Hede-r
Hea-dr
Header
Hadere

There are 5 lines, so that would be 5 different headers. Now, the number of headers will change so the number 5 is not fixed. This is why I cannot keep using one fprintf() command per header because I don't know ahead of time how many headers will be present in the file textf.txt. So, here is how I am hoping to do this:

fid2 = fopen('textf.txt'); %open the file containing the headers
mydata = textscan(fid, '%s'); %extract headers into a n X 1 cell (in this case, n = 5)
fclose(fid2);
[n_rows,n_cols]=mydata{1}; %assign number of headers to variable n_rows (n_cols will be 1)

fid1 = fopen(...); %open file for printing headers
for ijk = 1:n_rows
fprintf(' ' mydata{1}(ijk) '\n') %I need help with this command
end
fclose(fid1); %close file to which headers were printed

Questions:
(1) The length of the headers (always 6 characters) is such that I need to add a certain number of spaces, in order to align them with columns of numbers sitting beneath it. However, I am manually adding those spaces in. Is there a way for me to specify the number of spaces, rather than manually having to put them in? For example, if I need 9 spaces, rather than saying:

' Headr'

is there a way to say

'Headr'

with some argument value set to 9?

(2) is mydata{1}(ijk) correct?

Subject: fprintf not correctly aligning numbers and text

From: Eli

Date: 13 Jul, 2012 01:01:57

Message: 18 of 20

> ' Headr'
>
I typed in ' Headr' but there should be 9 spaces, not 1 space as it is being displayed. I typed in 9 spaces, but for some reason only 1 space is being displayed in the post. It should be 9 everywhere.

Subject: fprintf not correctly aligning numbers and text

From: dpb

Date: 13 Jul, 2012 04:17:31

Message: 19 of 20

On 7/12/2012 7:39 PM, Eli wrote:
...

> Questions:
> (1) The length of the headers (always 6 characters) is such that I need
> to add a certain number of spaces, in order to align them with columns
> of numbers sitting beneath it. However, I am manually adding those
> spaces in. Is there a way for me to specify the number of spaces, rather
> than manually having to put them in? For example, if I need 9 spaces,
> rather than saying:
>
> ' Headr'
>
> is there a way to say
>
> 'Headr'
>
> with some argument value set to 9?

I would have thought by now the obvious would have become so...use a
dynamic field width to match the field width desired...

 >> hdr={'123456'}; 'An identifiable length header
 >> for i=1:60,fprintf('%d',mod(i-1,10));end,fprintf('\n');for
i=1:6,fprintf(['%' num2str(i+5) 's'],char(hdr));end

012345678901234567890123456789012345678901234567890123456789
123456 123456 123456 123456 123456 123456
 >>

Obviously you can dynamically add blanks if it helps somewhere as well;
I didn't follow the actual want/need outline clearly enough to be able
to see otomh whether that would make any sense or not...but if so, sure,
do whatever...

doc blanks % may be of interest in that case...

> (2) is mydata{1}(ijk) correct?

My version/release of Matlab predates the double-indexing syntax--you'll
have to work that out on your own; I can only guess...if it gives what
you want, then my guess is it's right, otherwise I'll let you guess what
my guess would be... :)

--

Subject: fprintf not correctly aligning numbers and text

From: Eli

Date: 13 Jul, 2012 16:49:13

Message: 20 of 20

Okay, it worked. That was a much a more efficient way for me to be inserting the spaces.

Thanks for all the help.

My questions in this thread have been answered.

Tags for this Thread

Everyone's Tags:

Add a New Tag:

Separated by commas
Ex.: root locus, bode

What are tags?

A tag is like a keyword or category label associated with each thread. Tags make it easier for you to find threads of interest.

Anyone can tag a thread. Tags are public and visible to everyone.

Tag Activity for This Thread
Tag Applied By Date/Time
fprintf Eli 5 Jul, 2012 13:24:08
sprintf Eli 5 Jul, 2012 13:24:08
f Eli 5 Jul, 2012 13:24:08
s Eli 5 Jul, 2012 13:24:08
repmat Eli 5 Jul, 2012 13:24:08
rssFeed for this Thread

Contact us