|
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.
|