I have 06 popup menu and I want to get its values ​​to calculate DATE_START and DATE_END

1 view (last 30 days)
hi everyone, I have 06 popup menu in GUI and I want to get its values ​​to calculate
date_start = [da,dm,dj];
date_end = [fa,fm,fj];
to use them in a push button
here is the script of each push button sata all
% --- Executes on selection change in d_annee.
function d_annee_Callback(hObject, eventdata, handles)
contents=cellstr(get(hObject,'String'));
popda=contents(get(hObject,'Value'));
da=num2str(cell2mat(popda));
% --- Executes on selection change in d_mois.
function d_mois_Callback(hObject, eventdata, handles)
contents=cellstr(get(hObject,'String'));
popdm=contents(get(hObject,'Value'));
popdm;
if(strcmp(popdm,'Janvier'));
dm='01'
elseif(strcmp(popdm,'Février'));
dm='02'
elseif(strcmp(popdm,'Mars'));
dm='03'
elseif(strcmp(popdm,'Avril'));
dm='04'
elseif(strcmp(popdm,'Mai'));
dm='05'
elseif(strcmp(popdm,'Juin'));
dm='06'
elseif(strcmp(popdm,'Juillet'));
dm='07'
elseif(strcmp(popdm,'Août'));
dm='08'
elseif(strcmp(popdm,'Septembre'));
dm='09'
elseif(strcmp(popdm,'Octobre'));
dm='10'
elseif(strcmp(popdm,'Novembre'));
dm='11'
elseif(strcmp(popdm,'Décembre'));
dm='12'
end
% --- Executes on selection change in d_jour.
function d_jour_Callback(hObject, eventdata, handles)
contents=cellstr(get(hObject,'String'));
popdj=contents(get(hObject,'Value'));
dj=num2str(cell2mat(popdj));
% --- Executes on selection change in f_annee.
function f_annee_Callback(hObject, eventdata, handles)
contents=cellstr(get(hObject,'String'));
popfa=contents(get(hObject,'Value'));
fa=num2str(cell2mat(popfa))
% --- Executes on selection change in f_mois.
function f_mois_Callback(hObject, eventdata, handles)
contents=cellstr(get(hObject,'String'));
popfm=contents(get(hObject,'Value'));
popfm;
if(strcmp(popfm,'Janvier'));
fm='01'
elseif(strcmp(popfm,'Février'));
fm='02'
elseif(strcmp(popfm,'Mars'));
fm='03'
elseif(strcmp(popfm,'Avril'));
fm='04'
elseif(strcmp(popfm,'Mai'));
fm='05'
elseif(strcmp(popfm,'Juin'));
fm='06'
elseif(strcmp(popfm,'Juillet'));
fm='07'
elseif(strcmp(popfm,'Août'));
fm='08'
elseif(strcmp(popfm,'Septembre'));
fm='09'
elseif(strcmp(popfm,'Octobre'));
fm='10'
elseif(strcmp(popfm,'Novembre'));
fm='11'
elseif(strcmp(popfm,'Décembre'));
fm='12'
end
% --- Executes on selection change in f_jour.
function f_jour_Callback(hObject, eventdata, handles)
contents=cellstr(get(hObject,'String'));
popfj=contents(get(hObject,'Value'));
fj=num2str(cell2mat(popfj));
% --- Executes on button press in stat_all.
function stat_all_Callback(hObject, eventdata, handles)
% hObject handle to stat_all (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
global dj dm da fj fm fa
handles=guidata(gcbf);
date_start=[da,dm,dj];
date_end=[fa,fm,fj];

Accepted Answer

Geoff Hayes
Geoff Hayes on 14 Feb 2019
Mustapha - if you are going to use GUIDE to build your GUI, then rather than using global variables to share information between callbacks, use the handles structure to save the dm or fm data and then access this data from another callback. For example, in your d_mois_Callback callback, do
function d_mois_Callback(hObject, eventdata, handles)
contents=cellstr(get(hObject,'String'));
popdm=contents(get(hObject,'Value'));
popdm;
if(strcmp(popdm,'Janvier'));
dm='01';
elseif ...
end
handles.dm = dm;
guidata(hObject, handles); % this saves the updated handles structure
Then in your pushbutton callback you would do
function stat_all_Callback(hObject, eventdata, handles)
dm = handles.dm;
fm = ...; % etc.
end
Note that you may want to check for the existence of these fields (dm, fm, etc.) before you try to access them. Or assign default values to these fields in the OpeningFcn of your GUI. Also, you have duplicate code in your two popup menu callbacks. Since you know
contents=cellstr(get(hObject,'String'));
popfm=contents(get(hObject,'Value'));
then perhaps you could just find popfm in contents and use its index as your dm or fm value (assuming that your list of months are in the usual order).

More Answers (0)

Categories

Find more on Simulink Functions in Help Center and File Exchange

Tags

Products


Release

R2014b

Community Treasure Hunt

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

Start Hunting!