Re: Writing aligned data



roberson@xxxxxxxxxxxxxxxxxx (Walter Roberson) wrote in
message <g4b13n$o4k$1@xxxxxxxxxxxxxxxxxxxxxxx>...
In article <g4atua$rc8$1@xxxxxxxxxxxxxxxxxx>,
Hesham <heltaher.nospam@xxxxxxxxxxxxx> wrote:
Please, I have a small questions.
I want to write numeric data to a file, but the problem is
that I want the numbers to be written at specific places as
in the following example:
$# nid x y

1 0.000 0.02500000
254 -0.02377641 -0.00772543
So, as you can see the numbers should be right aligned under
each of the letters.

Use a format of '%8d%16.8f%20.8f\n'

S = '$# nid x y';
V = [1 0 0.025;254 -0.02377641 -0.00772543];
disp(sprintf('%s',S));
disp(sprintf('%8d%16.8f%20.8f\n',V'))
$# nid x y
1 0.00000000 0.02500000
254 -0.02377641 -0.00772543


I do note that the first x value is not in the format you
requested (not three digits after the decimal), but there
is no
single format element that expresses "Use at least 3
decimal places
but no more than 8". If it is important that there be the
3 decimal
places in one case but 8 in the other, then you will
either have to
concatenate together pre-formatted strings, or else use
awkward
constructs such as,

disp(sprintf('%s',S));
disp(sprintf('%8d%16.*f%20.8f\n',V(1,1),3,V(1,2),V(1,3),V(2,1),8,V(2,2),V(2,3)))

or, more compactly and generalizably,

T = num2cell([V(:,1),[3 8]',V(:,2:end)])';
disp(sprintf('%s',S));
disp(sprintf('%8d%16.*f%20.8f\n',T{:}))
$# nid x y
1 0.000 0.02500000
254 -0.02377641 -0.00772543

The [3 8] in the T line is the insertion of the
user-required number of
decimal places for the element that will follow in the list.
--
"Ignorance has been our king... he sits unchallenged on
the throne of
Man. His dynasty is age-old. His right to rule is now
considered
legitimate. Past sages have affirmed it. They did
nothing to unseat
him." -- Walter M
Miller, Jr

It worked.
Thank you very much,
Best Regards,
Hesham

.



Relevant Pages

  • Re: trouble exporting *.mat to ascii
    ... ASCII file. ... but it will let me open the individual structs. ... Is there a way to get the whole *.mat file into a text format? ...
    (comp.soft-sys.matlab)
  • Formatting Numbers
    ... I'm importing data from an access database into an Excel spreadsheet. ... a column that contains numeric and alpha numeric data. ... format the numeric data so that it is in number format (currently it imports ... format of the alphanumeric data manually as there are only two or three ...
    (microsoft.public.excel.worksheet.functions)
  • Re: Excel importing of csv and xml
    ... You can certainly automate the process - start by recording a macro ... XL imports according to the MS Spreadsheet schema. ... want to import numbers in a different format, ... > comes in as numeric data of 1503 in both formats, ...
    (microsoft.public.excel)
  • Re: MISSING ZERO IN IMPORTED EXCEL DATA 104.10 = 104.1 ????HOW CAN
    ... My VB App converts the numeric data into text because I need to ... >> When importing from Excel (or any other external file format, ... Wherever you want to show the two decimals (or ...
    (microsoft.public.access.externaldata)
  • RE: Formatting Numbers
    ... "Frank Kabel" wrote: ... >>a column that contains numeric and alpha numeric data. ... >>format the numeric data so that it is in number format ... >>happens until I edit an individual cell. ...
    (microsoft.public.excel.worksheet.functions)