Thread Subject: realy strange converting of numbers,need help

Subject: realy strange converting of numbers,need help

From: ETF Markovic

Date: 21 Mar, 2010 23:14:03

Message: 1 of 2

i need help with this problem:
i have for example this number
-1.72294676303864e-08
and i need to convert this number in this form:
-.172294676303D-07
and 91 to
.910000000000D+02

and write it to .txt file

sorry for my bad English.thanks

Subject: realy strange converting of numbers,need help

From: Walter Roberson

Date: 22 Mar, 2010 00:14:38

Message: 2 of 2

ETF Markovic wrote:
> i need help with this problem:
> i have for example this number
> -1.72294676303864e-08
> and i need to convert this number in this form:
> -.172294676303D-07
> and 91 to
> .910000000000D+02
>
> and write it to .txt file


Astr = num2str(A(:),'%.12e');
Astr(:,end-3) = 'D';
Astr = Astr(:,[1:end-18 end-16 end-17 end-15:end]);
Acell = cellfun(@strtrim, cellstr(Astr));
Acell = cellfun(@(s) ...
  sprintf('%s%+.2d', s(1:end-3), str2double(s(end-2:end))+1), Acell);

fid = fopen('output.txt', 'wt');
fprintf(fid, '%s\n', Acell{:});
fclose(fid);

This code takes into account the possibility that all of the numbers
might be positive. When all of the numbers are positive, num2str will
not leave any space for the implied +, so the decimal point can be in
either column 3 (if there at least one of the numbers is negative) or in
column 2 (if all of the numbers are negative.) Rather than use a
separate condition to test whether there is a space for the sign or not,
I instead coded positions backwards from the end of the string.

When 'e' format is used, the numbers will end with 'e' followed by a
sign followed by two digits. That's the same format you want except you
want the e replaced by 'D' -- so we can just go ahead and overwrite that
column.

1:end-18 will be the space or '-' if the at least one of the numbers was
negative, and will be the empty vector if all of the numbers are
positive. Then end-16 followed by end-17 exchanges the next two columns,
moving the decimal point before the first digit. end-15:end then carries
to the end of the string.

The next line of manipulation has to do with the fact that you do not
want a leading space or leading + for positive numbers. That implies
that we may have varying numbers of columns per line, and the only way
to implement that is by using a cell array. cellstr() converts the
character array into a cell array, and then cellfun(@strtrim) applied to
that cell array runs through all of the rows and removes leading blanks,
leaving us with a cell array containing strings that might not all be
equal in length.

The second cellfun call could probably be converted to operate on the
array instead, but then we would need to worry about horzcat(), making
it a little uglier. Anyhow, what it does is take the sign and following
two digits, converts that to a number, adds 1 to that, and converts the
result back into a signed two digit integer. This adjustment is needed
because when we move the decimal point one place to the left, we need to
increment the (signed) exponent.

To print out that cell array, fprintf() is used; the %s\n format says to
print out one string and then put an end-of-line marker. That format
will be repeated for all of the arguments, and Acell{:} expands to all
of those (variable-length) strings just as if each one of them had been
typed in as an individual argument to fprintf(). Hence, all the numbers
will be printed, one per line.

The code could be made clearer and simpler and faster if it was
acceptable to output leading + on positive numbers.

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
realy ETF Markovic 21 Mar, 2010 19:19:04
strange ETF Markovic 21 Mar, 2010 19:19:04
format ETF Markovic 21 Mar, 2010 19:19:04
number ETF Markovic 21 Mar, 2010 19:19:04
rssFeed for this Thread

Contact us at files@mathworks.com