MATLAB: How to set value in slider bar as in 2 decimal point

decimal pointsmatlab guislider

Hi,
I have create the slider which is in the range of 0 to 1 with interval 0.01 and 100 steps. Then, every time I change the slider, the value will display on the static text and save it as .mat file. I have made the value on static text as in 2 decimal point. However, the value got from the slider was different. For instance, the value display on static text is 0.63, but the value from slider is 0.6349. I want my value from the slider exactly same as what display on the static text. What function that I can applied?
This my code:
function slider_threshold_Callback(hObject, eventdata, handles)
global Pth
% hObject handle to slider_threshold (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
% Hints: get(hObject,'Value') returns position of slider
% get(hObject,'Min') and get(hObject,'Max') to determine range of slider
% set the slider range and step size
set(handles.slider_threshold, 'Min', 0);
set(handles.slider_threshold, 'Max', 1);
set(handles.slider_threshold, 'SliderStep', [0.01 , 0.99 ]); % 100 steps
%save the current/last slider value as the threshold value
Pth = get(handles.slider_threshold, 'Value');
set(handles.Threshold_text,'String', num2str(Pth,'%04.2f'));
save ('Pth.mat','Pth');
% Update handles structure
guidata(hObject, handles);

Best Answer

Pth = str2double( get(handles.Threshold_text, 'String') );
However, keep in mind that binary floating point arithmetic is not able to exactly represent any two-digit decimal numbers except for those that end in .00, .25, .50, and .75 . IEEE 754 Double Precision literally cannot exactly represent 0.63 . The closest value that it is able to exactly represent is 0.63000000000000000444089209850062616169452667236328125 . If you need the stored value to be exactly 0.63 then you are going to need to either use the Fixed Point Toolbox or the Symbolic Toolbox.
Related Question