how to check edit textbox for character arguements

2 visualizaciones (últimos 30 días)
Nicholas Mun
Nicholas Mun el 13 de Abr. de 2022
Respondida: Image Analyst el 13 de Abr. de 2022
I want to make a textboc input where if the input is '1' function a will excecute, and if input is '2' then function b will execute, but i am getting an error in the if arguement. here is the complete code
function varargout = FOPfinal(varargin)
% FOPFINAL MATLAB code for FOPfinal.fig
% FOPFINAL, by itself, creates a new FOPFINAL or raises the existing
% singleton*.
%
% H = FOPFINAL returns the handle to a new FOPFINAL or the handle to
% the existing singleton*.
%
% FOPFINAL('CALLBACK',hObject,eventData,handles,...) calls the local
% function named CALLBACK in FOPFINAL.M with the given input arguments.
%
% FOPFINAL('Property','Value',...) creates a new FOPFINAL or raises the
% existing singleton*. Starting from the left, property value pairs are
% applied to the GUI before FOPfinal_OpeningFcn gets called. An
% unrecognized property name or invalid value makes property application
% stop. All inputs are passed to FOPfinal_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 FOPfinal
% Last Modified by GUIDE v2.5 13-Apr-2022 11:59:36
% Begin initialization code - DO NOT EDIT
gui_Singleton = 1;
gui_State = struct('gui_Name', mfilename, ...
'gui_Singleton', gui_Singleton, ...
'gui_OpeningFcn', @FOPfinal_OpeningFcn, ...
'gui_OutputFcn', @FOPfinal_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 FOPfinal is made visible.
function FOPfinal_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 FOPfinal (see VARARGIN)
% Choose default command line output for FOPfinal
handles.output = hObject;
% Update handles structure
guidata(hObject, handles);
% UIWAIT makes FOPfinal wait for user response (see UIRESUME)
% uiwait(handles.figure1);
% --- Outputs from this function are returned to the command line.
function varargout = FOPfinal_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 pushbutton1.
function pushbutton1_Callback(hObject, eventdata, handles)
errordlg ('Covert any text into morse code with this app! To decode morse code back into text, press the SWITCH button','Help me')
% hObject handle to pushbutton1 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
function text_input_Callback(hObject, eventdata, handles)
% hObject handle to text_input (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 text_input as text
% str2double(get(hObject,'String')) returns contents of text_input as a double
text_input=get(hObject,'string');
handles.text_input=text_input;
guidata(hObject,handles);
% --- Executes during object creation, after text_setting all properties.
function text_input_CreateFcn(hObject, eventdata, handles)
% hObject handle to text_input (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
% --- Executes on button press in pushbutton2.
function pushbutton2_Callback(hObject, eventdata, handles)
% hObject handle to pushbutton2 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
setting_text = get(handles.text_setting);
userinput = handles.text_input;
moutput = '';
if setting_text == 1
userinput = upper(userinput);
userinput = strjoin(strsplit(userinput));
morsecode={'.----','..---','...--','....-','.....','-....','--...','---..','----.','-----','.-','-...','-.-.','-..','.','..-.','--.','....','..','.---','-.-','.-..','--','-.','---','.--.','--.-','.-.','...','-','..-','...-','.--','-..-','-.--','--..','/'};
NumberOrLetter={'1','2','3','4','5','6','7','8','9','0','A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P','Q','R','S','T','U','V','W','X','Y','Z',' '};
for i=1:length(userinput);
[~, index] = ismember(userinput(i), NumberOrLetter);
if index > 0
moutput = [moutput ' ' morsecode{index}];
end
end
set(handles.morse, 'String' , moutput);
end
if setting_text == 2
morsecode = {'.-','-...','-.-.','-..','.','..-.','--.','....','..','.---','-.-','.-..', ...
'--','-.','---','.--.','--.-','.-.','...','-','..-','...-','.--','-..-', ...
'-.--','--..'};
letter = char('A':'Z');
symbol = strsplit(userinput, ' ');
for i = 1:length(symbol)
moutput = [moutput ' ' morsecode{i}]
end
set(handles.morse, 'String' , moutput);
end
function text_setting_Callback(hObject, eventdata, handles)
% hObject handle to text_setting (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 text_setting as text
% str2double(get(hObject,'String')) returns contents of text_setting as a double
text_setting=get(hObject,'string');
handles.text_setting=text_setting;
guidata(hObject,handles);
% --- Executes during object creation, after text_setting all properties.
function text_setting_CreateFcn(hObject, eventdata, handles)
% hObject handle to text_setting (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
% --- Executes on button press in pushbutton4.
function pushbutton4_Callback(hObject, eventdata, handles)
errordlg ('To convert text into morse code, enter "1" into the box, and to convert morse code back into text please enter "2" into the box','Tutorial')
% hObject handle to pushbutton4 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
This is the part of the code where the if function arguement is used. the error says that '==' is not a supported operand type of 'struct' but i dont know how to fix that
if setting_text == 1
userinput = upper(userinput);
userinput = strjoin(strsplit(userinput));
morsecode={'.----','..---','...--','....-','.....','-....','--...','---..','----.','-----','.-','-...','-.-.','-..','.','..-.','--.','....','..','.---','-.-','.-..','--','-.','---','.--.','--.-','.-.','...','-','..-','...-','.--','-..-','-.--','--..','/'};
NumberOrLetter={'1','2','3','4','5','6','7','8','9','0','A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P','Q','R','S','T','U','V','W','X','Y','Z',' '};
for i=1:length(userinput);
[~, index] = ismember(userinput(i), NumberOrLetter);
if index > 0
moutput = [moutput ' ' morsecode{index}];
end
end
set(handles.morse, 'String' , moutput);
end

Respuesta aceptada

Walter Roberson
Walter Roberson el 13 de Abr. de 2022
Editada: Walter Roberson el 13 de Abr. de 2022
text_setting=get(hObject,'string');
It says right there, text_setting will be a string (well, really, character vector rather than string() object.)
if setting_text == 1
You are comparing the character vector to a numeric value. That is not likely to do what you want.
Have you considered using a checkbox or pushbutton or listbox instead of a uicontrol 'edit' ? If you use a checkbox or pushbutton then you would fetch the 'Value' property from the object, and it would be 0 (not checked, not pushed) or 1 (checked, or pushed), or it would be 1 through the number of entries in the listbox (unless the user had not selected anything, in which case it could be empty.)
If for some reason you want to continue using a uicontrol style 'edit' then you need
text_setting = str2double(get(hObject, 'String'));
Note: the 'String' property for a uicontrol checkbox or pushbutton is the label for the control, and has nothing to do with the state of the control. The 'String' property for a dropbox or listbox is the list of labels, in order, and does not reflect which one the user has chosen.
  1 comentario
Nicholas Mun
Nicholas Mun el 13 de Abr. de 2022
thanks the checkbox version is much more easier to understand and does fuffill what i need it to do! youve beena great help! :)

Iniciar sesión para comentar.

Más respuestas (1)

Image Analyst
Image Analyst el 13 de Abr. de 2022
if handles.setting_text.Value % If the checkbox is checked...

Categorías

Más información sobre Migrate GUIDE Apps en Help Center y File Exchange.

Etiquetas

Community Treasure Hunt

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

Start Hunting!

Translated by