Display default variable in edit box that can be modified in GUI

Hello! I have an edit box that is linked to a popmenu. For example, if I select parameter1 in the popmenu, the editbox displays a default value value1. If parameter2 is selected in the popmenu, value2 is displayed in the editbox. The unit is also displayed in another textbox as shown in the image below. I also need that all the default values in the editbox can be modified and appear in the workspace to be used later.
This is my code:
function param_menu_Callback(hObject, eventdata, handles)
global parametre
parametre=get(handles.param_menu, 'Value');
if parametre==1
set(handles.edit_vmin, 'String' , '----');
set(handles.unit_text, 'String', {''}); % sets the units in the unit_text
elseif parametre==2
set(handles.edit_vmin, 'String' , '1000'); %sets filtering values of the chosen parameter
set(handles.unit_text, 'String', {'Tours/s'}); % sets the units in the unit_text
elseif parametre==3
set(handles.edit_vmin, 'String', '100');
set(handles.unit_text, 'String', {'kW'});
elseif parametre==4
set(handles.edit_vmin, 'String', '9.5');
set(handles.unit_text, 'String', {'Tours/s'});
elseif parametre==5
set(handles.edit_vmin, 'String', '5');
set(handles.unit_text, 'String', {'m/s'});
end
function edit_vmin_Callback(hObject, eventdata, handles)
global valeur_min
valeur_min=str2double(get(handles.edit_vmin,'String'));
It gives me "value_min" in the workspace (double) (that i can use in another file).
When I change the value (remove the default value of editbox and choose another one)and start the analysis, the value goes back to the default value and not the one i've entered. I would like to be able to have a default value and to still be able to choose one. Thank you again for your time!

6 Comments

Don't use global variables. These are just the type of problems you can get by parachuting variables into your workspace and writing to them and then parachuting them in somewhere else etc.
How much 'later on' do you need the values to appear in your workspace? Can't you use the workspace of the GUI as would normally be the case when you have a GUI that takes inputs?
It doesn't really make much logical sense to change variables in a GUI and then have them in the base workspace. There would seem to be no advantage over just editing them directly in the base workspace. Usually you would have a GUI doing this where the calculations you want to do also take place from a GUI callback (and usually present results in some form or other) so it is self-contained.
I use valeur_min in another matlab file which analyses data (valeur min is the minimal value accepted in some data). I am not sure that I was very clear. My Gui should be used by people how don't know how to program, and i need that when I press a "analyse" button, the value that is displayed in the edit box (default or entered) is available. The value will be used in another matlab file to filter data.
I'm sorry i'm new to matlab, that's why i used global variables, because it seemed to work.
Edit: I also use a R2013a version of matlab
Take a look at the link I included above. You should be able to store your data on the handles structure to pass around between the callbacks of the UI e.g.
function edit_vmin_Callback(hObject, eventdata, handles)
handles.valeur_min = str2double(get(handles.edit_vmin,'String'));
guidata( hObject, handles )
Then you can have a callback for your Analyse pushbutton which can use
handles.valueur_min
and it will be what you set it to. Make sure that whenever you add anything to handles or edit any of the things you add there that you include that
guidata( hObject, handles )
line though, otherwise the changes you make are just local to the callback and get lost.
You should also set default values in your OpeningFcn for the fields you add to handles to cover the case where someone just presses Analyse without editing the value.
Thank you for your answer but I still have a problem. I get the default value just fine, but if I change it, I still get the default value and not the modified one.
function desk_OpeningFcn(hObject, eventdata, handles, varargin)
% Choose default command line output for desk
handles.output = hObject;
guidata(hObject, handles);
function update_date_button_Callback(hObject, eventdata, handles)
param_menu_Callback(handles.param_menu, [], handles)
edit_vmin_Callback(handles.edit_vmin, [], handles)
handles.valeur_min
function param_menu_Callback(hObject, eventdata, handles)
handles.parametre=get(handles.param_menu, 'Value');
if handles.parametre==1
set(handles.edit_vmin, 'String' , '----');
set(handles.unit_text, 'String', {''}); % sets the units in the unit_text
elseif handles.parametre==2
set(handles.unit_text, 'String', {'Tours/s'}); % sets the units in the unit_text
set(handles.edit_vmin, 'String' , '1000'); %sets filtering values of the chosen parameter
elseif handles.parametre==3
set(handles.edit_vmin, 'String', '100');
set(handles.unit_text, 'String', {'kW'});
elseif handles.parametre==4
set(handles.edit_vmin, 'String', '9.5');
set(handles.unit_text, 'String', {'Tours/s'});
elseif handles.parametre==5
set(handles.edit_vmin, 'String', '5');
set(handles.unit_text, 'String', {'m/s'});
end
guidata(hObject,handles)
function edit_vmin_Callback(hObject, eventdata, handles)
handles.valeur_min=str2double(get(handles.edit_vmin,'String'));
guidata( hObject, handles)
Thank you again
Do you press enter or tab or something similar after editing the value? An edit callback won't trigger if you just type the number in. I'm not sure if it would trigger before the pushbutton callback if you move away and click on that, I assume not.
I solved the problem with this:
function update_date_button_Callback(hObject, eventdata, handles)
%edit_vmin_Callback(handles.edit_vmin, [], handles)
param_menu_Callback(handles.param_menu, [], handles)
set(handles.edit_vmin,'String', handles.valeur_min);

Sign in to comment.

Answers (0)

Community Treasure Hunt

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

Start Hunting!