Using an Edit Box as an output in a GUI

9 views (last 30 days)
nick asby
nick asby on 28 Apr 2016
Commented: Jan on 2 May 2016
Hello all!
I've been attempting to create a GUI that does 3 things.
1. Push a 'push button'
2. Load a .m file
3. Put the contents of that .m file as an 'output' into an edit text box.
The .m file (which is called TestAll) returns one value - a string called "ProbDiag." Now I'm trying to make it that whenever you click the button, the value of ProbDiag (where ProbDiag = 'Sinus Tachycardia') is outputted into the edit box.
The .m file is being brought in completely fine, but I'm running into issues 'pushing' that value into the edit box. Any help??? the 2 callback function codes are attached below
% --- Executes on button press in test_button.
function test_button_Callback(hObject, eventdata, handles)
% hObject handle to test_button (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
TestAll; %runs the .m file called TestAll as long as it's in the same directory
function results_Callback(hObject, eventdata, handles)
% hObject handle to results (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 results as text
% str2double(get(hObject,'String')) returns contents of results as a double
set(handles.results, 'String',ProbDiag)
  1 Comment
Jan
Jan on 28 Apr 2016
Does it matter how the variable is called, which carries the output of the function? You do not have to call it "ProbDiag", when I understand the problem correctly.

Sign in to comment.

Answers (3)

Walter Roberson
Walter Roberson on 28 Apr 2016
  1 Comment
nick asby
nick asby on 28 Apr 2016
Edited: nick asby on 28 Apr 2016
Can you elaborate on what you meant by this? I'm still a little confused on how this helps. In order to make the GUI do what I want, I think I'm supposed to maybe move that last chunk of code (set.handles...) up to the Callback function for the push button.
When I do that though it returns an error about "reference to a cleared variable handles."
What I want to do is similar to this (I believe). https://www.mathworks.com/matlabcentral/newsreader/view_thread/30058

Sign in to comment.


Image Analyst
Image Analyst on 28 Apr 2016
You say TestAll() returns one value - a string called "ProbDiag." but you don't even accept it as a return value and then you don't do anything with it. Make the function do this:
% --- Executes on button press in test_button.
function test_button_Callback(hObject, eventdata, handles)
% hObject handle to test_button (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
ProbDiag = TestAll; %runs the .m file called TestAll as long as it's in the same directory
% Put ProbDiag string into the results control
handles.results.String = ProbDiag;
  1 Comment
Jan
Jan on 28 Apr 2016
And store the handles struct in the GUI afterwards:
guidata(hObject, handles);

Sign in to comment.


Jan
Jan on 28 Apr 2016
function test_button_Callback(hObject, eventdata, handles)
handles.Result = TestAll;
guidata(hObject, handles);
function results_Callback(hObject, eventdata, handles)
handles = guidata(hObject); % I assume we need this here for the current value
set(handles.results, 'String', handles.Result)
  2 Comments
nick asby
nick asby on 28 Apr 2016
Jan - I went ahead and tried this - but it returned this error message once I hit the push button.
>> GUI
Attempt to execute SCRIPT TestAll as a function: /Users/Asby/Desktop/TestAll.m
Error in GUI>test_button_Callback (line 82) handles.Result = TestAll;
Error in gui_mainfcn (line 95) feval(varargin{:});
Error in GUI (line 42) gui_mainfcn(gui_State, varargin{:});
Error in @(hObject,eventdata)GUI('test_button_Callback',hObject,eventdata,guidata(hObject))
Error while evaluating UIControl Callback
Here's my entire code (besides the initializing stuff GUIDE automatically puts in).
% --- Executes just before GUI is made visible.
function GUI_OpeningFcn(hObject, eventdata, handles, varargin)
% This function has no output args, see OutputFcn.
% hObject handle to figure
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
% varargin command line arguments to GUI (see VARARGIN)
% Choose default command line output for GUI
handles.output = hObject;
% Update handles structure
guidata(hObject, handles);
% UIWAIT makes GUI wait for user response (see UIRESUME)
% uiwait(handles.figure1);
% --- Outputs from this function are returned to the command line.
function varargout = GUI_OutputFcn(hObject, eventdata, handles)
% varargout cell array for returning output args (see VARARGOUT);
% hObject handle to figure
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
% Get default command line output from handles structure
varargout{1} = handles.output;
% --- Executes on button press in test_button.
function test_button_Callback(hObject, eventdata, handles)
% hObject handle to test_button (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
%TestAll; %runs the .m file called TestAll as long as it's in the same directory
handles.Result = TestAll;
guidata(hObject, handles);
function results_Callback(hObject, eventdata, handles)
% hObject handle to results (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 results as text
% str2double(get(hObject,'String')) returns contents of results as a double
handles = guidata(hObject);
set(handles.results, 'String', handles.Result)
% --- Executes during object creation, after setting all properties.
function results_CreateFcn(hObject, eventdata, handles)
% hObject handle to results (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles empty - handles not created until after all CreateFcns called
% Hint: edit controls usually have a white background on Windows.
% See ISPC and COMPUTER.
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
set(hObject,'BackgroundColor','white');
end
Jan
Jan on 2 May 2016
@nick: If your file "TestAll.m" is a script file (this means, that it does not start with "function ...", you cannot assign an output variable as in handles.Result = TestAll . Scripts do not have outputs. Look for "scripts" and "functions" in the documentation.

Sign in to comment.

Categories

Find more on Encryption / Cryptography 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!