Issue on sprintf in Matlab - matlab

How to print in Matlab Like following....
0.01000E+02
I have tried
sprintf('%12.5e',[0.01000E+02])
it is giving me
1.00000e+000

You format is a bit specific. You should consider writing your own output function.
But a few pointers:
Make e large with upper
only 2 digits in exp number through a regexp. new_string = regexprep(old_string,'\d(\d{2})$','\1')
the thing with leading 0 in exp representation is not standard - so maybe multiply with 1e2, print the float and later attach the E+02.

Something like ['0.0' strrep(sprintf('%12.5E',v*100), '.', '')] (with v your value) should work if I understand correctly your format.

Related

MATLAB - literally convert decimal to string

I hope I have a simple question, I just couldn't figure it out.
I have several numbers which I want to be converted to string quite literally:
12.000 -> '12.000'
4.0 -> '4.0'
34.760000 -> '34.760000'
As you can see, I cannot simply pad zeros, since that highly depends on how many zero are given with the number.
Does anyone know how to do this?
Ahh, yes, this is easily accomplished with MATLAB's num2str function, like so:
num2str(12.000 ,'%.3f')
num2str(4.0, '%.1f')
num2str(34.760000,'%.6f')
WRT " %x.f ", where x equals 3,1, and 6 in the examples above, this is called the formSpec, which I would encourage you to read about more, here. In this case, we are saying that the variable is a floating point number, and we want to preserve x digits after the decimal place. It is useful to know about format specification for parsing text, and to efficiently read from and write to files.
Edit: A point of clarification, and as I'm sure you already know, single quotes (' ') in MATLAB yield a character array rather than a string. These are different data types. If you're really after a string, just add string to the num2str argument, i.e.,
string(num2str(12.000 ,'%.3f'))
string(num2str(4.0, '%.1f'))
string(num2str(34.760000,'%.6f'))

Octave / Matlab - Reading fixed width file

I have a fixed width file format (original was input for a Fortran routine). Several lines of the file look like the below:
1078.0711005.481 932.978 861.159 788.103 716.076
How this actually should read:
1078.071 1005.481 932.978 861.159 788.103 716.076
I have tried various methods, textscan, fgetl, fscanf etc, however the problem I have is, as seen above, sometimes because of the fixed width of the original files there is no whitespace between some of the numbers. I cant seem to find a way to read them directly and I cant change the original format.
The best I have come up with so far is to use fgetl which reads the whole line in, then I reshape the result into an 8,6 array
A=fgetl
A=reshape(A,8,6)
which generates the following result
11
009877
703681
852186
......
049110
787507
118936
So now I have the above and thought I might be able to concatenate the rows of that array together to form each number, although that is seeming difficult as well having tried strcat, vertcat etc.
All of that seems a long way round so was hoping for some better suggestions.
Thanks.
If you can rely on three decimal numbers you can use a simple regular expression to generate the missing blanks:
s = '1078.0711005.481 932.978 861.159 788.103 716.076';
s = regexprep(s, '(\.\d\d\d)', '$1 ');
c = textscan(s, '%f');
Now c{1} contains your numbers. This will also work if s is in fact the whole file instead of one line.
You haven't mentioned which class of output you needed, but I guess you need to read doubles from the file to do some calculations. I assume you are able to read your file since you have results of reshape() function already. However, using reshape() function will not be efficient for your case since your variables are not fixed sized (i.e 1078.071 and 932.978).
If I did't misunderstand your problem:
Your data is squashed in some parts (i.e 1078.0711005.481 instead
of 1078.071 1005.481).
Fractional part of variables have 3 digits.
First of all we need to get rid of spaces from the string array:
A = A(~ismember(A,' '));
Then using the information that fractional parts are 3 digits:
iter = length(strfind(A, '.'));
for k=1:iter
[stat,ind] = ismember('.', A);
B(k)=str2double(A(1:ind+3));
A = A(ind+4:end);
end
B will be an array of doubles as a result.

How does one convert from char format to double format, when working with binary numbers?

I have a piece of code which outputs what I want but in the wrong format
for k=1:100
bin(k,:)=dec2bin(randi([0 31]),5);
end
I want the output to be a 100x5 double array, with one bit per cell (0 or 1 value).
I've tried using the double() function...
for k=1:100
bin(k,:)=double(dec2bin(randi([0 31]),5));
end
...but that returns the correct format, with the wrong values.
My jargon might be a bit off, I apologise (Am I using cell, double, etc in the wrong context?)
Thank you for helping me.
There are a lot of ways to do what you want. The simplest would actually be generating the binary array right from the start, without a loop:
bin = rand(100, 5) > 0.5
Other alternatives:
If you have an integer array and you want to convert it to bits, you can use bitget instead of dec2bin inside the loop:
bin(k, :) = bitget(randi([0 31]), 5:-1:1)
If you already have a string array representing binary numbers, and you want to operate on it, you can delimit the bits with spaces and then apply str2num:
bin = reshape(str2num(sprintf('%c ', bin)), size(bin))

MATLAB num2str format

I'm storing variable values in MATLAB and putting one of the variable values as part of the file name . e.g. "Error=1e-003.mat", however different version of MATLAB gives me different format when I'm converting numbers to string using num2str command. The number 1e-3, MATLAB2011 gives me 1e-003 and MATLAB2012 gives me 1e-03.
This runs into trouble when I try to load a batch of files with a mix of those two format. Does anyone know a way to add a zero or delete a zero for the exponent so the names are consistent? Or any other ways to solve this problem?
Here's a fairly robust way to do it:
A = num2str(YOUR_NUMBER, '%0.0e');
if A(end-2)=='-' || A(end-2)=='+'
A = [A(1:end-2) '0' A(end-1:end)]; end
In words: convert the number, and check if the second-to-last character is either a '+' or a '-'. If this is so, add a '0'.
Specify a "Format string as the second argument like this:
>> disp(num2str(2920230,'%0.10e'))
2.9202300000e+006
here %0.10e means display at least 0 digits before decimal and exactly 10 digits after it in exponent form.

MATLAB: ??? Undefined function or method 'sprint' for input arguments of type 'char'

I´m trying to show 16 decimal place of a result. The code I put is this
clear x;
x = 0.245;
1-x+1/2*x.^2-1/6*x.^3+1/24*x.^4
sprint('%0.16f', ans)
Matlab give me this answer
ans =
0.7827
??? Undefined function or method 'sprint' for input arguments of type 'char'.
I have two question:
What happen? I think I used it before and I had no problems with 'sprintf' for show a result with several decimal places.
What can I do to show more decimal places?
Thank you!
sprintf formats data into a string; it does not display it for output. Furthermore, it's sprintf, not sprint, which is the function you've typed- and that MATLAB is complaining about. (It doesn't know what sprint is, but it knows about sprintf.)
If you mean to save ans to a string as a number to 16 decimal places, use sprintf. To just display it, which I think is what you want, use printf instead. In either case, the issue is clear; you forgot the f in sprintf!
Well, I think 'vpa' this help me to show more decimal places
clear x;
clear expresion;
x = 0.245;
expresion = 1-x+1/2*x.^2-1/6*x.^3+1/24*x.^4
%sprint('%0.16f', ans)
vpa(expresion,16)
EDIT:
and this is the matlab answer:
expresion =
0.7827
ans =
.7827116041927082
I think you did not use sprint before. There is no MATLAB intrinsic function called sprint, you ought to use sprintf.