format short, format long, format short E, format long E, format rat

How can i represent a number let's say 1231312.1232323 with the formats long/short/long e /short e /rat more elegant or in a single code line without having to write 10 colums i was thinking of the fprintf and format spec commands . P.S i'm very much a beginner in matlab :) .

 채택된 답변

For a single value I would suggest the fprintf function:
v=1234567.1234567;
fprintf('%.7f\n',v)
1234567.1234567

댓글 수: 4

so in conclusion what i'm hoping for is not possible ?
format short
a, b, c
format long
a,b,c
format short e
a,b,c
format long e
a,b,c
format rat
a,b,c
i did it like this ,was wondering if i could do it all in a single command line
I'n not sure why you would want that, but sure:
a=rand;b=rand;c=rand;
for f={'short','long','shortEng','longEng','rat'}
format(f{1})
a,b,c
end
Which you can put in a single line if you insist:
for f={'short','long','shortEng','longEng','rat'},format(f{1}),a,b,c,end
thx :) quote " I'n not sure why you would want that, but sure "
why not ? for diversity and cause it's fun learning new stuff and providing different answears /methods to the same question than the expected ones .
Maybe it is just that I prefer having full control by using fprintf or sprintf, and I don't see the benefit of spamming the command window with the same value in different formats.
But at least you have an example here of how you can convert one syntax for format to another, and how you can use any object array in a for loop, which will then be processed column by column.

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

추가 답변 (0개)

카테고리

도움말 센터File Exchange에서 Loops and Conditional Statements에 대해 자세히 알아보기

제품

질문:

2020년 10월 28일

편집:

Rik
2020년 10월 28일

Community Treasure Hunt

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

Start Hunting!

Translated by