이 질문을 팔로우합니다.
- 팔로우하는 게시물 피드에서 업데이트를 확인할 수 있습니다.
- 정보 수신 기본 설정에 따라 이메일을 받을 수 있습니다.
Change amount of significant figures or precision in array elements
조회 수: 39 (최근 30일)
이전 댓글 표시
Is there a way to do this?
Example: In LabVIEW there is a thing called width and if you specify a width of 5 for some number, the maximum number of digits it can have will be 5. Is this possible in MATLAB?
채택된 답변
Star Strider
2018년 10월 2일
If you absolutely must do that, see the section in the round (link) function documentation on: Round Elements to Specified Number of Significant Digits (link).
That is likely not your best option. I would do the calculations with the data you have, and round the result to the requisite number of significant digits.
댓글 수: 17
PromoCodeBSJ
2018년 10월 2일
I have to put measurement data into a table that will go into a report generated by the report generator toolkit. Currently the measurement data will give me something like 4.69000000001 and I just want that to become 4.69. Will your answer do the trick?
Star Strider
2018년 10월 2일
If this is any indication, it will:
x = 4.69000000001
y = round(x, 3, 'significant')
x =
4.69000000001
y =
4.69
PromoCodeBSJ
2018년 10월 2일
What am I doing wrong?
RawDataChapter=Chapter('Raw Measurement Data');
b=size(Data)
Data
Data=array2table(round(Data,5'significant'));
Data=MATLABTable(Data);
add(RawDataChapter,Data);
James Tursa
2018년 10월 2일
You are missing a comma between the 5 and the 'significant'
round(Data,5,'significant')
PromoCodeBSJ
2018년 10월 2일
편집: PromoCodeBSJ
2018년 10월 2일
By the way, your code does not work. If you put format long and then run your code it just has a lot of 0s tacked behind it.
edit: actually maybe thats because i used format long
Star Strider
2018년 10월 2일
Your array2table call (at least the one you posted) is missing a comma:
Data=array2table(round(Data,5'significant'));
↑
Data=array2table(round(Data,5,'significant'));
That aside, since (as I understand it), table data types inherit the current format setting, perhaps setting:
format short g
or whatever is appropriate, somewhere in your code before you display your table could work.
James Tursa
2018년 10월 2일
Note that 4.69 cannot be represented exactly in IEEE double precision format. E.g.,
>> num2strexact(4.69)
ans =
4.69000000000000039079850466805510222911834716796875
Star Strider
2018년 10월 2일
That is the result of ‘floating-point approximation error’.
The format short g and format long g options take this into account. Use one of them, and your table should display correctly.
PromoCodeBSJ
2018년 10월 4일
The format short g/ long g are not working. My report still comes out with a ridiculous amount of numbers.
RawDataChapter=Chapter('Raw Measurement Data');
b=size(Data);
format short g
Data=array2table(round(Data,5,'significant'));
Data=MATLABTable(Data);
add(RawDataChapter,Data);
Star Strider
2018년 10월 4일
OK. I do not have the MATLAB Report Generator. When I look through the online documentation, I cannot find anything about formatting the precision of the numerical entries in a MATLABTable.
This seems to me to be a significant oversight. I encourage you to report this as a bug. Use the Contact Us link in the upper right corner of this page, and in your message include the URL of this thread, so you don’t have to repeat the entire discussion and description.
Also, when you do that, have MATLAB open, and first run the ver command from a script or from your Command Window. You will need to copy and paste the output to the second window in your message.
Please post back here with anything of interest in the reply MathWorks sends you.
PromoCodeBSJ
2018년 10월 5일
This is the response I got from Support:
Hello Eric,
Thank you for contacting MathWorks Technical Support Department. My name is Aaron and I am writing in reference to your Technical Support Case #03271769 regarding 'Formatting precision in MATLABTable'.
I understand that you would like to specify the precision displayed in a MATLAB Table when including the table in a generated report. Thank you for sharing the link to the MATLAB Answers forum page.
I have been in contact with the developers of MATLAB about this potential capability in the MATLAB Report Generator. Unfortunately, it is not currently possible to specify the precision of a table in the MATLAB Report Generator. The developers of MATLAB are currently considering implementing this feature in a future release of the program.
In the meantime, there are a couple of potential workarounds to consider:
1. Consider first rounding the entries of the table using the 'round' function and then converting the rounded numbers to strings. This will ensure that the table entries are displayed in the desired precision. For an example of how to do this, please refer to the attached file named 'TablePrecisionRound.m' and the comments in the file.
For more information about the 'round' function, please refer to the following link:
https://www.mathworks.com/help/matlab/ref/round.html
2. Consider using the 'compose' function with a specified number format in order to convert the numbers to string arrays. By using the 'compose' function, a custom precision or number format can be easily specified. As an example of how to do this in MATLAB, please refer to the attached file named 'TablePrecisionCompose.m' and the comments in the file.
For more information about the 'compose' function, please refer to the following link:
https://www.mathworks.com/help/matlab/ref/compose.html
Please note that I certainly understand that each of the workarounds above are not ideal. You can be assured that the developers are considering implementing the feature in a future release.
For now, I will tentatively close this case from an administrative perspective. However, please let me know if there are any additional related questions as I would be happy to re-open the case and assist.
Sincerely,
Aaron Zakrzewski MathWorks Technical Support Department
Star Strider
2018년 10월 5일
Thank you for posting that.
I’m relieved to know that the formatting capability doesn’t exist, and that I didn’t miss something.
추가 답변 (0개)
참고 항목
카테고리
Help Center 및 File Exchange에서 Continuous Waveforms에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!오류 발생
페이지가 변경되었기 때문에 동작을 완료할 수 없습니다. 업데이트된 상태를 보려면 페이지를 다시 불러오십시오.
웹사이트 선택
번역된 콘텐츠를 보고 지역별 이벤트와 혜택을 살펴보려면 웹사이트를 선택하십시오. 현재 계신 지역에 따라 다음 웹사이트를 권장합니다:
또한 다음 목록에서 웹사이트를 선택하실 수도 있습니다.
사이트 성능 최적화 방법
최고의 사이트 성능을 위해 중국 사이트(중국어 또는 영어)를 선택하십시오. 현재 계신 지역에서는 다른 국가의 MathWorks 사이트 방문이 최적화되지 않았습니다.
미주
- América Latina (Español)
- Canada (English)
- United States (English)
유럽
- Belgium (English)
- Denmark (English)
- Deutschland (Deutsch)
- España (Español)
- Finland (English)
- France (Français)
- Ireland (English)
- Italia (Italiano)
- Luxembourg (English)
- Netherlands (English)
- Norway (English)
- Österreich (Deutsch)
- Portugal (English)
- Sweden (English)
- Switzerland
- United Kingdom(English)
아시아 태평양
- Australia (English)
- India (English)
- New Zealand (English)
- 中国
- 日本Japanese (日本語)
- 한국Korean (한국어)