From: Walter Roberson on
Erik de Boer wrote:

> To analyse a fracture mechanics problem I'm using a FEM program that
> requires an imported mesh (set of nodes and coordinates + their
> connectivity). I've written a matlab script to create this mesh array.
>
> The FEM program can read the array from a .txt file. For the program to
> be able to read it, the vertical offset between the last character in
> column n and the decimal separartor of culumn n+1 has to be 5 spaces.
> Like this:
> 24 9.0000 6.0000 25 0
> 0 30 1.2857 -0.4286 31 3.8571
> -1.2857
> Up to now I wasn't able to do this, using dlmwrite and fprintf.
> Admittedly, I'm not very familiar with these commands. So my question
> is: how do I write my matlab data to a .txt file with fixed no. of
> spaces between decimal seperator of consecutive columns?
> I hope someone can help me with this. Thanks!

dlmwrite() 'Precision' parameter or appropriate fprintf() format.

You want fixed-point so you want a '%f' format. 4 decimal places so the
format needs '.4', making it '%.4f'. Now you need to calculate the total
width of the field, which is the 5 characters before the decimal, plus
the decimal, plus the 4 after, making 10 total. This gives you a format
of '%10.4f' if you are using fprintf.

If you are using dlmwrite() then you need to change the delimiter from
the default. You could use space as a delimiter, but if you do then you
need to reduce the width of the field to account for the space, giving
you '%9.4f'. Better for your purpose is likely to change the delimiter
to '' (the empty string) and retain %10.4f .

The exact format you should use would depend on whether

6.0000-9999.9999

would be considered an error (due to lack of space between the fields)
or would be considered to be 6.0000 followed by -9999.9999 . Usually if
there is an exact spacing requirement imposed (as in this case) there is
no requirement or expectation for a space.