Display default variable in edit box that can be modified in GUI
Show older comments
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
Adam
on 2 Jun 2017
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.
Christine Abou Nasr
on 2 Jun 2017
Edited: Christine Abou Nasr
on 2 Jun 2017
Adam
on 2 Jun 2017
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.
Christine Abou Nasr
on 6 Jun 2017
Edited: Christine Abou Nasr
on 6 Jun 2017
Adam
on 6 Jun 2017
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.
Christine Abou Nasr
on 6 Jun 2017
Edited: Christine Abou Nasr
on 6 Jun 2017
Answers (0)
Categories
Find more on Whos in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!