Change sympref with shortcut line of code

조회 수: 2 (최근 30일)
Evan Jones
Evan Jones 2021년 10월 21일
댓글: Evan Jones 2021년 10월 25일
I want to change whether a symbolic equation outputs a decimal or fraction without having to type out/copy & paste the full line "sympref('FloatingPointOutput',false)"
It would be nice to have a line where I type "frac" or "dec" or some other shorthand to change this.
Here's a very quick example:
u = symunit
u =
symbolicUnitsCollection with units: ampere: [A] kelvin: [K] kilogram: [kg] meter: [m] mole: [mol] second: [s] candela: [cd] Show all units.
syms D_in D_out L k
sympref('FloatingPointOutput',false)
ans = logical
0
Rcond = log((D_out/2)/(D_in/2))*(1/(2*pi*L*k))
Rcond = 
sympref('FloatingPointOutput',true)
ans = logical
0
D_in = 11.75*u.in
D_in = 

채택된 답변

Steven Lord
Steven Lord 2021년 10월 21일
You could create a simple function to change the preference. You could even have that function retrieve the current value and set the preference to the other value (to toggle it.)
syms x
for k = 1:5
toggleFPO
queryFPO
y = expand((x*k).^(1/3))
end
FloatingPointOutput is enabled.
y = 
FloatingPointOutput is disabled.
y = 
FloatingPointOutput is enabled.
y = 
FloatingPointOutput is disabled.
y = 
FloatingPointOutput is enabled.
y = 
function toggleFPO
sympref('FloatingPointOutput', ~sympref('FloatingPointOutput'));
end
function queryFPO
if sympref('FloatingPointOutput')
fprintf('FloatingPointOutput is enabled.\n')
else
fprintf('FloatingPointOutput is disabled.\n')
end
end

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Number Theory에 대해 자세히 알아보기

태그

제품


릴리스

R2021b

Community Treasure Hunt

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

Start Hunting!

Translated by