0

Right now, I am working on a GUI that plots graphs based on user-input data. The user enters numbers using edit boxes and from there the plot is generated.

I have two values, period and frequency, which mathematically I know are interdependent (p=1/f and vice-versa). I believe I have made it so that when I edit frequency, a new value for period is calculated, but what I also want to do is display the new value for period in the period edit box whenever the user types in a new value for frequency. To illustrate, here is the code that I have for the frequency edit box right now:

%period (ns)
function edit11_Callback(hObject, eventdata, handles)
num = str2double(get(hObject,'string'));
%global edit10;%brings frequency to edit11 function
%global edit11;%globalize period
handles.edit11 = num;
handles.edit10 = 1/num;%frequency = 1/period
set(handles.edit10,'String',num2str(1/num));%displays new frequency
guidata(hObject,handles)

note: edit10 is the edit box for period. I expect that the code for that one would just be a sort of reverse of what I have for frequency. Also, I commented out the 'global' lines because it doesn't seem that I need them for what I'm trying to do, but I did try that out.

"handles.edit10 = 1/num;" updates the actual value for period. As far as I know, the last line (guidata(...)) will save the new values for handles.edit10 and handles.edit11 globally, but correct me if that's wrong, please.

What doesn't work is the line: "set(handles.edit10,'String',num2str(1/num));", which is supposed to display the new value for period in the period edit box (edit10).

The error message I get is as follows:

Error using handle.handle/set
Invalid or deleted object.

Error in Liposome_GUI>edit11_Callback (line 367)
set(handles.edit10,'String',num2str(1/num));%displays new frequency

Error in gui_mainfcn (line 95)
    feval(varargin{:});

Error in Liposome_GUI (line 42)
gui_mainfcn(gui_State, varargin{:});

Error in @(hObject,eventdata)Liposome_GUI('edit11_Callback',hObject,eventdata,guidata(hObject))


Error while evaluating uicontrol Callback

The error message makes it seem like the function edit11 doesn't have access to the value stored in handles.edit10, but nothing seems to change if I put those two 'global' lines in under both the edit10 and edit11 functions.

Can anybody tell me what I'm doing wrong?

Jehmil
  • 1
  • 1

2 Answers2

0

what happens if you remove the over-writing of the handle for edit10? also you don't need to overwrite edit11 either... nor save the handles here

the following will work when there is a callback on edit11

%period (ns)
function edit11_Callback(hObject, eventdata, handles)
  num = str2double(get(handles.edit11,'string'));
  set(handles.edit10,'String',num2str(1/num)); %displays new frequency
end
codekitty
  • 1,438
  • 3
  • 20
  • 30
  • Hm, if I try that, I get the message: Illegal use of reserved keyword "end". If I take out that 'end' line, I still get the same error message as before. – Jehmil Feb 11 '15 at 00:33
  • If it helps, here is the full code for 'edit11': Hm, if I try that, I get the message: Illegal use of reserved keyword "end". If I take out that 'end' line, I still get the same error message as before. This is the full code for edit11, if it helps: function edit11_Callback(hObject, eventdata, handles) num = str2double(get(handles.edit11,'string')); set(handles.edit10,'String',num2str(1/num)); function edit11_CreateFcn(hObject, eventdata, handles) if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor')) set(hObject,'BackgroundColor','white'); end – Jehmil Feb 11 '15 at 00:42
0

I think the error is generated because you overwrite the value assigned to the uicontrol edit10 using this command in the edit11_Callback on line 7:

handles.edit10 = 1/num;%frequency = 1/period

Then MATLAB thinks the actual edit box handles has been deleted because every data associated with it has be overwritten with the variable num.

Otherwise your code should work. I made a simple GUI in case you would like to play around with edit boxes. What it does is simply update the edit boxes and a plot of dummy data dependent on the period/frequency. Everything is commented so it should be easy to follow. If there is something unclear please tell me.

function GUI_EditBox
clc
clear
close all

%// Create GUI components
hFigure = figure('Position',[100 100 500 500],'Units','Pixels');

handles.axes1 = axes('Units','Pixels','Position',[60,90,400,300]);

handles.TextPeriod = uicontrol('Style','Text','Position',[20 470 60 20],'String','Period');
handles.TestFrequ = uicontrol('Style','Text','Position',[20 440 60 20],'String','Frequency');

handles.EditPeriod = uicontrol('Style','Edit','Position',[100 470 60 20],'String','2*pi','Value',1,'Callback',@(s,e) EditPeriodCallback);
handles.EditFrequ = uicontrol('Style','Edit','Position',[100 440 60 20],'String','pi/2','Callback',@(s,e) EditFrequCallback);

%// Define initial period and frequency
handles.Period = 2*pi;
handles.Frequency = 1/handles.Period;

%// Define x data and function to plot
handles.x = 0:pi/10:2*pi;

handles.y = rand(1)*sin(handles.Period.*handles.x);

plot(handles.x,handles.y,'Parent',handles.axes1)

guidata(hFigure,handles); %// Save handles structure of GUI.

%// The callbacks for both edit boxes are quite similar. Basically get the value entered by the user and update the other as well as the plot.
    function EditPeriodCallback(~,~)

        handles = guidata(hFigure);

        handles.Period = str2double(get(handles.EditPeriod,'String'));
        handles.Frequency = 1/handles.Period;

        set(handles.EditFrequ,'String',num2str(1/handles.Period));

        handles.y = 2*rand(1)*sin(handles.Period.*handles.x);
        plot(handles.x,handles.y,'Parent',handles.axes1)


        guidata(hFigure,handles);
    end

    function EditFrequCallback(~,~)

        handles = guidata(hFigure);

        handles.Frequency = str2double(get(handles.EditFrequ,'String'));
        handles.Period = 1/handles.Frequency;

        set(handles.EditPeriod,'String',num2str(1/handles.Frequency));

        handles.y = 2*rand(1)*sin(handles.Period.*handles.x);
        plot(handles.x,handles.y,'Parent',handles.axes1)


        guidata(hFigure,handles);
    end

end 

Sample screenshot of the GUI:

enter image description here

Note that it would probably be useful to add a box in which the user could provide a multiple of pi or something like that instead of integers.

Hope that helps!

Benoit_11
  • 13,905
  • 2
  • 24
  • 35