필터 지우기
필터 지우기

convert a numbere from integer to double

조회 수: 357 (최근 30일)
Natalia Molinero Mingorance
Natalia Molinero Mingorance 2015년 3월 18일
댓글: Andrew Gerlach 2020년 9월 29일
Hi,
does anyone know how can I covert 1 in 1.0000, please?
Thank you!
  댓글 수: 4
James Tursa
James Tursa 2015년 3월 20일
So, the real problem isn't conversion ... the real problem is that you need to write a text file in a specific format? That was not mentioned at all in your original post. Please tell us the size and type of the variable(s) involved, and the exact text format you want in the file.
Andrew Gerlach
Andrew Gerlach 2020년 9월 29일
Most of the time Matlab handles this fine, but there are cases when you need to explicitly convert a number type. For example, I just loaded imaging data from a .nii file. When I tried to get the standard deviation from a time series, I got the following error:
>> std(ts)
Error using var (line 74)
Invalid data type. First input argument must be single or double.
Error in std (line 59)
y = sqrt(var(varargin{:}));
The fix was to convert to double manually:
>> std(double(ts))
ans =
128.1063

댓글을 달려면 로그인하십시오.

답변 (3개)

James Tursa
James Tursa 2015년 3월 18일
편집: James Tursa 2015년 3월 18일
Your question is not clear. If the number is a scalar double and exactly 1, it will display as 1 without the trailing 0's. If it is very close to 1 but not exactly 1, it will display with 0's. E.g.,
>> 1
ans =
1
>> 1+eps(1)
ans =
1.0000
To get more digits to display you can use format. E.g.,
>> format long
>> 1
ans =
1
>> 1+eps(1)
ans =
1.000000000000000
If you really have an integer type and need to convert it to double type, use the double function. E.g.,
>> k = int32(1)
k =
1
>> class(k)
ans =
int32
>> d = double(k)
d =
1
>> class(d)
ans =
double
  댓글 수: 1
Natalia Molinero Mingorance
Natalia Molinero Mingorance 2015년 3월 20일
I don't really understand how to use those function, I mean, the syntax: if if got a vector x=[1,1,1], how can I convert it into x=[1.00000, 1.0000, 1.0000]? Thank you!!

댓글을 달려면 로그인하십시오.


Stephen23
Stephen23 2015년 3월 20일
편집: Stephen23 2015년 3월 20일
Here is one easy way that you can save a vector of values with your chosen precision: with dlmwrite:
>> A = [-1, 1, 1, -1];
>> dlmwrite('temp.txt',A, 'precision','%.4f');
produces me a file which includes this text:
-1.0000,1.0000,1.0000,-1.0000
See the fprintf documentation for a list of the precision formatting options.
  댓글 수: 4
Natalia Molinero Mingorance
Natalia Molinero Mingorance 2015년 3월 23일
that's fine, but I just want the space before the -1 and that will insert the space before the 1s too, right? :( thank you!
Stephen23
Stephen23 2015년 3월 23일
편집: Stephen23 2015년 3월 23일
If you want to control the spaces individually then you will need to use a lower-level function, such as fprintf:
>> A = [-1, 1, 1, -1];
>> fid = fopen('temp.txt','wt');
>> fprintf(fid,' %.4f,%.4f\n',A);
>> fclose(fid);
gives me a file with this inside (there is a space in front of the first column):
-1.0000,1.0000
1.0000,-1.0000
Note that fprintf will simply keep applying that formatting to the input variables until it runs out of variable... and this is columnwise! You might need to transpose the data before inputting it into fprintf.
If this does not resolve your formatting issues, please upload an example file with the exact formatting that you need, and I will show you how to generate it using dlmwrite or fprintf.

댓글을 달려면 로그인하십시오.


Shantanu Jana
Shantanu Jana 2015년 3월 23일
편집: Shantanu Jana 2015년 3월 23일
you can do this process and latter use 'ans' as variable that hold your answer
>> a=1
a =
1
>>a = double(a)
>> sprintf('%0.4f', a)
ans =
1.0000
>>
  댓글 수: 2
Guillaume
Guillaume 2015년 3월 23일
>>a = 1
creates a of type double. Therefore:
>>a = double(a)
does nothing whatsoever.
Stephen23
Stephen23 2015년 3월 23일
편집: Stephen23 2015년 3월 23일
Lets try the suggested code:
>> a = 1;
>> class(a)
ans = double
Lo! we already have a double! Which means that the code suggested by Shantanu Jana:
>> a = double(a);
does nothing useful at all, as a is already of class double.
MATLAB's documentation about floating point numbers states that "...The default is double precision...". My first comment to the original question also clarifies this.

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Characters and Strings에 대해 자세히 알아보기

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by