In: Electrical Engineering
In MatLab ( this question is after a lecture about GUI )
% How do you read a property from a control (i.e. the
% 'string' property from an edit box)?
follow the following step :
step1 :open gui by typinging guide in matlab command prommt.
step02 :Take one edit box and set its values as shown by clicking on it and i have changed tag property .
step03) now save it and type following command in m file.
function varargout = untitled1(varargin)
% UNTITLED1 MATLAB code for untitled1.fig
% UNTITLED1, by itself, creates a new UNTITLED1 or raises the
existing
% singleton*.
%
% H = UNTITLED1 returns the handle to a new UNTITLED1 or the handle
to
% the existing singleton*.
%
% UNTITLED1('CALLBACK',hObject,eventData,handles,...) calls the
local
% function named CALLBACK in UNTITLED1.M with the given input
arguments.
%
% UNTITLED1('Property','Value',...) creates a new UNTITLED1 or
raises the
% existing singleton*. Starting from the left, property value pairs
are
% applied to the GUI before untitled1_OpeningFcn gets called.
An
% unrecognized property name or invalid value makes property
application
% stop. All inputs are passed to untitled1_OpeningFcn via
varargin.
%
% *See GUI Options on GUIDE's Tools menu. Choose "GUI allows only
one
% instance to run (singleton)".
%
% See also: GUIDE, GUIDATA, GUIHANDLES
% Edit the above text to modify the response to help untitled1
% Last Modified by GUIDE v2.5 11-Apr-2018 08:02:05
% Begin initialization code - DO NOT EDIT
gui_Singleton = 1;
gui_State = struct('gui_Name', mfilename, ...
'gui_Singleton', gui_Singleton, ...
'gui_OpeningFcn', @untitled1_OpeningFcn, ...
'gui_OutputFcn', @untitled1_OutputFcn, ...
'gui_LayoutFcn', [] , ...
'gui_Callback', []);
if nargin && ischar(varargin{1})
gui_State.gui_Callback = str2func(varargin{1});
end
if nargout
[varargout{1:nargout}] = gui_mainfcn(gui_State, varargin{:});
else
gui_mainfcn(gui_State, varargin{:});
end
% End initialization code - DO NOT EDIT
% --- Executes just before untitled1 is made visible.
function untitled1_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 untitled1 (see VARARGIN)
% Choose default command line output for untitled1
handles.output = hObject;
% Update handles structure
guidata(hObject, handles);
% UIWAIT makes untitled1 wait for user response (see
UIRESUME)
% uiwait(handles.figure1);
% --- Executes during object creation, after setting all
properties.
function edituserinput_CreateFcn(hObject, eventdata, handles)
% hObject handle to edituserinput (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
function edituserinput_Callback(hObject, eventdata, handles)
% hObject handle to edituserinput (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 edituserinput
as text
% str2double(get(hObject,'String')) returns contents of
edituserinput as a double
%below two lines are very important
%line one reads the string data type from user
%line2 store that data from variable userinput in gui to variable name userdata in matlab workspace
userinput=get(handles.edituserinput,'string');
assignin('base','userdata',userinput);
RESULT:
I have typed hi .
Note you get variable named userdata having string hi stored in workspace.