How to make a value limit condition to a edit text and slider in GUI?

2 Ansichten (letzte 30 Tage)
I made a GUI that contains 1 slider (min=-180,max=180) and 1 edit text my goal is to have the user type the angle wanted in the edit text or instead use the slider to choose a value. but i found a problem: if the user typed a value bigger than 180 or smaller than -180, the slider vanishes.. I want to show a pop error window when that happens and reset the value of the edit text and the slider to max (or min) value .. I've wrote a code, but it seems that it's not working:
function Valeur_Theta1_Callback(hObject, eventdata, handles)
% hObject handle to Valeur_Theta1 (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,'String') returns contents of Valeur_Theta1 as text
% str2double(get(hObject,'String')) returns contents of Valeur_Theta1 as a double
clear min max Theta1;
min=-180;max=180;
Theta1=str2double(get(handles.Valeur_Theta1,'string'));
if(Theta1>-180 || Theta1<180)
{
set(handles.Slider_Theta1,'value',Theta1);}
else if(Theta1<-180)
{
msgbox('Invalid Value', 'Error','error');
set(handles.Slider_Theta1,'value',min);
set(handles.Valeur_Theta1,'string',min);
}
else if(Theta1<-180)
{
msgbox('Invalid Value', 'Error','error');
set(handles.Slider_Theta1,'value',max);
set(handles.Valeur_Theta1,'string',max);
}
end
end
end
please help!

Akzeptierte Antwort

Image Analyst
Image Analyst am 6 Sep. 2015
You DEFINITELY don't want to clear the min and max function!!!!!!! Not only that, but you go on to redefine them as something else. VERY DANGEROUS to redefine crucial built-in functions such as min() and max()!
Plus, it wasn't working because you used braces to stick the outputs of all your lines of code into a cell. Please read the FAQ on cells: http://matlab.wikia.com/wiki/FAQ#What_is_a_cell_array.3F. If you put braces around something, you're putting it into a cell. This is not C you know.
So just clip the values to -180 or +180 and carry on:
minSliderValue = get(handles.Slider_Theta1, 'Min');
maxSliderValue = get(handles.Slider_Theta1, 'Max');
if Theta1 < minSliderValue
Theta1 = minSliderValue ;
elseif Theta1 > maxSliderValue
Theta1 = maxSliderValue;
end
% Set slider value.
set(handles.Slider_Theta1, 'Value', Theta1);
% Set static text caption of slider.
caption = sprintf('Theta = %.2f', Theta1);
set(handles.Valeur_Theta1, 'String', caption);
Note I didn't use 180 or -180 anywhere, so this code is robust enough to handle any min or max that you might enter into GUIDE. If you did that and I had used 180, the code would break. This way is more robust because you only need to change the angle limits in one place and it carries through elsewhere.

Weitere Antworten (1)

Med Aymane Ahajjam
Med Aymane Ahajjam am 6 Sep. 2015
Appreciate your detailed answer Image Analyst :D I got it (and I read the FAQ ) and it works like charm! (y)

Kategorien

Mehr zu Migrate GUIDE Apps finden Sie in Help Center und File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by