gui executing input parameter

Hello,
I am working on gui (created with Guide). I have problem in finding the way to execute the change of parameter that is used later in the code to calculate some outputs. My gui has two Buttons and Edit Text field.
Button_1 opens the file with the exteranla data
Button_2 reads the data and makes some calculations
Edit Text field is used to insert the parameter that is used in calculations (by pressing Button_2)
My intension is to be able to change the parameter in Edit Text so that it is exectued by pressing Button_2 without the need of reopining the file (with Button_1). For now it seems that each time I do motifiaction to the Edit Text I have to open the file (with Button_1) and not only redo the calulations (with Button_2).
Below is part of my gui:
% --- Executes on button press in OpenFile.
function OpenFile_Callback(hObject, eventdata, handles)
handles=OpenFile(handles);
guidata(hObject, handles);
function input_p_Callback(hObject, eventdata, handles)
% hObject handle to input_p (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 input_p as text
% str2double(get(hObject,'String')) returns contents of input_p as a double
% --- Executes during object creation, after setting all properties.
function input_p_CreateFcn(hObject, eventdata, handles)
% hObject handle to input_p (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles empty - handles not created until after all CreateFcns called
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
set(hObject,'BackgroundColor','white');
end
% --- Executes on button press in Read.
function Read_Callback(hObject, eventdata, handles)
p = str2double(get(handles.input_p,'String'));
set(handles.input_p, 'String',num2str(p));
handles.p=p;
handles=Read(handles);
guidata(hObject, handles);

1 Comment

Can you attach the .m and .fig files? We might not need to spend another 10 messages over another 8 hours if you do.

Sign in to comment.

 Accepted Answer

Geoff Hayes
Geoff Hayes on 8 Oct 2021
@AStro you may need to post more code as it isn't clear what you might do with the data you read. But whatever it is, you can save the read data to the handles structure and then use that in your second button callback.

14 Comments

AStro
AStro on 8 Oct 2021
Edited: AStro on 8 Oct 2021
@Geoff Hayes Yes, I am calling-out and -in the data with handles, but I definitly have somewhere a bug that I am not able to trace down. Please see below the part of my code for the Button_1 and Button_2 from the gui.
Button_1 (OpenFile)
function handles=OpenFile(handles)
[open_file,open_pathdir] = uigetfile( ...
{'*.txt','File txt (*.txt)'}, ...
'Choose cwt file');
open_fnm = fullfile (open_pathdir, open_file);
handles.open_fnm=open_fnm;
Button_2 (Read)
function handles=Read(handles)
open_fnm=handles.open_fnm;
delimiterIn = ' ' ;
headerlinesIn = 0;
Data = importdata(open_fnm,delimiterIn,headerlinesIn);
L = Data.data(1,1);
Data = importdata(open_fnm);
freq = Data.data(:,2);
freq = freq/1e6;
ampl = Data.data(:,3)*10^3;
p = handles.p;
When I use gui it executes well input parameter p (set in the Edit Text) but if I change the parameter and press Button_2 again to redo calculations it seems lose the OpenFile handles. So every time I am changing paramter p I need to once again press OpenFile (this I want to avoid).
The error message is:
Error using handle.handle/get
Invalid or deleted object.
Error in CWT_software_gui>Read_Callback (line 107)
p = str2double(get(handles.input_p,'String'));
@AStro - where in your code do you assign something to handles.input_p? i.e. where is the code like
handles.input_p = ???;
hmm.. isn't this part of my gui that do the trick?
p = str2double(get(handles.input_p,'String'));
set(handles.input_p, 'String',num2str(p));
handles.p=p;
You are trying to get before you set so it makes sense that you see an error.
set(handles.input_p, 'String',num2str(p));
That assumes that handles.input_p already exists. It is possible that you created an object with tag input_p at the GUIDE level, so it might exist when the figure is opened. But... what parent did you use for it? If you used an axes parent for it, then there are number of different ways that something might have cleared the axes, If you used a figure or uipanel parent for it, then clf calls would be what I would look for first.
I have createed the object in gui that is input_p. It is the Edit Text field that I use to inser and modify the parameter. This parameter is then used in the function activated with Button_2. Therefore, I first used
p = str2double(get(handles.input_p,'String'))
to actually get the patameter from the Edit Text field of my gui and execute it in Button_2
I see that now...the input_p is a control. Why do you reset it though with
p = str2double(get(handles.input_p,'String'));
set(handles.input_p, 'String',num2str(p));
Wouldn't this just set input_p back to the same value?
Also, can you show the callback that calls OpenFile so that we can see how the handles structure is pased in?
As for now, whatever value I put to the Edit Text (input_p) is executed, but only for the first press of Button_2. When I press again Button_2 it does not recognize anymore the Edit Text, even if the value was unchanged. Perhaps the problem lies in the handles form Button_1, which are errased in the way? I am not sure though how to prevent it. Please see bellow the Button_1 (referring to the OpenFile function)
Button_1 (OpenFile)
% --- Executes on button press in OpenFile.
function OpenFile_Callback(hObject, eventdata, handles)
handles=OpenFile(handles);
guidata(hObject, handles);
Function for Button_1
function handles=OpenFile(handles)
[open_file,open_pathdir] = uigetfile( ...
{'*.txt','File txt (*.txt)'}, ...
'Choose cwt file');
open_fnm = fullfile (open_pathdir, open_file);
handles.open_fnm=open_fnm;
Button_2 (Read)
This incl. getting the parameter (input_p) from Edit Text field
% --- Executes on button press in Read.
function Read_Callback(hObject, eventdata, handles)
p = str2double(get(handles.input_p,'String'));
set(handles.input_p, 'String',num2str(p));
handles.p=p;
handles=Read(handles);
guidata(hObject, handles);
Function for Button_2
function handles=Read(handles)
open_fnm=handles.open_fnm;
p = handles.p;
handles=Read(handles);
What's that?
Perhaps it will be best if I provide the files (please see attachement)
@AStro - I think this is the code that is causing the error
p=str2double(get(handles.p,'String'));
set(handles.p, 'String',num2str(p));
handles.p=p;
handles=CWT_Read(handles);
% Update handles structure
guidata(hObject, handles);
Note that handles.p is the handle to the text control whose String value you are trying to get. You then set this String with the same value you had just retrieved with get. The code then does
handles.p=p;
where you are replacing the handle of p with the numeric value you obtained when converting the text string to a double. So you are overwriting the handle of p with some number (the default is 0.3 if unchanged in the GUI) which will not correspond to a valid GUI control and saving the updated handles structure when you call guidata. And so the next time when we try to call
p=str2double(get(handles.p,'String'));
handles.p is no longer valid and so the error message makes sense. Try removing the line
handles.p=p;
and try again.
Also, you may want to review some of your other functions to see if all are actually necessary (and do what you expect).
AStro
AStro on 11 Oct 2021
Edited: AStro on 11 Oct 2021
When the hanles.p=p is removed it does not recognize the parameter at all. It comes with an error message after very first push of the button Read.
I also noticed that the first button (OpenFile) works only once. Whenever I press it again then the second button (Read) display an error. Gui executes the buttons only after I close it and strat again.
I have attached again the code with an examplary data so that I you can test it out and see the issue :-)
@AStro - you have to remember that handles.p is the handle to the edit text control and by doing
handles.p=p;
you are replacing this handle with a numeric value that represents the minimum peak distance and so this will invalidate the handle. This line of code should be removed. I know you have said that this causes problems elsewhere in the code and so that code should be corrected. I see that in CWT_Read.m you do
p = handles.p;
This is incorrect as p is a handle. Replace this with
p=str2double(get(handles.p,'String'));
instead.
Thank you @Geoff Hayes it finally works :-) Thank you for your time all the way through

Sign in to comment.

More Answers (0)

Categories

Find more on Creating, Deleting, and Querying Graphics Objects in Help Center and File Exchange

Asked:

on 8 Oct 2021

Commented:

on 11 Oct 2021

Community Treasure Hunt

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

Start Hunting!