How to make morse code on GUI and display it on text box?

Hi I got the code but I'm stuck cause it says my morse doesnt exist. Below is the coding for the morse code on GUI. The problem right now is I cant diplay the result .
I think my code might be wrong. Really need help.
function varargout = morse(varargin)
% MORSE MATLAB code for morse.fig
% MORSE, by itself, creates a new MORSE or raises the existing
% singleton*.
%
% H = MORSE returns the handle to a new MORSE or the handle to
% the existing singleton*.
%
% MORSE('CALLBACK',hObject,eventData,handles,...) calls the local
% function named CALLBACK in MORSE.M with the given input arguments.
%
% MORSE('Property','Value',...) creates a new MORSE or raises the
% existing singleton*. Starting from the left, property value pairs are
% applied to the GUI before morse_OpeningFcn gets called. An
% unrecognized property name or invalid value makes property application
% stop. All inputs are passed to morse_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 morse
% Last Modified by GUIDE v2.5 07-Apr-2020 14:41:41
% Begin initialization code - DO NOT EDIT
gui_Singleton = 1;
gui_State = struct('gui_Name', mfilename, ...
'gui_Singleton', gui_Singleton, ...
'gui_OpeningFcn', @morse_OpeningFcn, ...
'gui_OutputFcn', @morse_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 morse is made visible.
function morse_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 morse (see VARARGIN)
% Choose default command line output for morse
handles.output = hObject;
% Update handles structure
guidata(hObject, handles);
% UIWAIT makes morse wait for user response (see UIRESUME)
% uiwait(handles.figure1);
% --- Outputs from this function are returned to the command line.
function varargout = morse_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;
function L_edit_Callback(hObject, eventdata, handles)
% hObject handle to L_edit (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 L_edit as text
% str2double(get(hObject,'String')) returns contents of L_edit as a double
letter=get(hObject,'string');
Letter_num=str2double(letter);
handles.Letter=letter;
guidata(hObject,handles)
% --- Executes during object creation, after setting all properties.
function L_edit_CreateFcn(hObject, eventdata, handles)
% hObject handle to L_edit (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 PB_M.
function PB_M_Callback(hObject, eventdata, handles)
% hObject handle to PB_M (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
Letter=handles.Letter
morse={'.----','..---','...--','....-','.....','-....','--...','---..','----.','-----','.-','-...','-.-.','-..','.','..-.','--.','....','..','.---','-.-','.-..','--','-.','---','.--.','--.-','.-.','...','-','..-','...-','.--','-..-','-.--','--..','/'};
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(Letter);
[~, index] = ismember(Letter(i), NumberOrLetter);
if index > 0
set(handle.Morse,sprintf,'string');
end
end
% --- Executes on button press in PB_L.
function PB_L_Callback(hObject, eventdata, handles)
% hObject handle to PB_L (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
Morse=handles.Morse
morse={'.----','..---','...--','....-','.....','-....','--...','---..','----.','-----','.-','-...','-.-.','-..','.','..-.','--.','....','..','.---','-.-','.-..','--','-.','---','.--.','--.-','.-.','...','-','..-','...-','.--','-..-','-.--','--..','/'};
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(Morse);
[~, index] = ismember(morse(i),NumberOrLetter);
if index > 0
set(handle.Morse,sprintf,'string');
end
end
function M_edit_Callback(hObject, eventdata, handles)
% hObject handle to M_edit (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 M_edit as text
% str2double(get(hObject,'String')) returns contents of M_edit as a double
Morse=get(hObject,'string');
Morse_num=str2double(Morse);
handles.Morse=Morse;
guidata(hObject,handles)
% --- Executes during object creation, after setting all properties.
function M_edit_CreateFcn(hObject, eventdata, handles)
% hObject handle to M_edit (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

2 commentaires

Siti - please copy and paste the full error message to this question. Do you have the figure/GUI or just the above code?
That is only the code for the GUI
I made some modification and this is the new code
Below are the errors that it gives me
Unrecognized function or variable 'M'.
Error in morse>M_edit_Callback (line 141)
handles.M=M;
Error in gui_mainfcn (line 95)
feval(varargin{:});
Error in morse (line 42)
gui_mainfcn(gui_State, varargin{:});
Error in
matlab.graphics.internal.figfile.FigFile/read>@(hObject,eventdata)morse('M_edit_Callback',hObject,eventdata,guidata(hObject))
Error while evaluating UIControl Callback.
Reference to non-existent field 'L'.
Error in morse>PB_M_Callback (line 107)
Get(handles.L,'string')
Error in gui_mainfcn (line 95)
feval(varargin{:});
Error in morse (line 42)
gui_mainfcn(gui_State, varargin{:});
Error in
matlab.graphics.internal.figfile.FigFile/read>@(hObject,eventdata)morse('PB_M_Callback',hObject,eventdata,guidata(hObject))
Error while evaluating UIControl Callback.
function varargout = morse(varargin)
% MORSE MATLAB code for morse.fig
% MORSE, by itself, creates a new MORSE or raises the existing
% singleton*.
%
% H = MORSE returns the handle to a new MORSE or the handle to
% the existing singleton*.
%
% MORSE('CALLBACK',hObject,eventData,handles,...) calls the local
% function named CALLBACK in MORSE.M with the given input arguments.
%
% MORSE('Property','Value',...) creates a new MORSE or raises the
% existing singleton*. Starting from the left, property value pairs are
% applied to the GUI before morse_OpeningFcn gets called. An
% unrecognized property name or invalid value makes property application
% stop. All inputs are passed to morse_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 morse
% Last Modified by GUIDE v2.5 07-Apr-2020 14:41:41
% Begin initialization code - DO NOT EDIT
gui_Singleton = 1;
gui_State = struct('gui_Name', mfilename, ...
'gui_Singleton', gui_Singleton, ...
'gui_OpeningFcn', @morse_OpeningFcn, ...
'gui_OutputFcn', @morse_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 morse is made visible.
function morse_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 morse (see VARARGIN)
% Choose default command line output for morse
handles.output = hObject;
% Update handles structure
guidata(hObject, handles);
% UIWAIT makes morse wait for user response (see UIRESUME)
% uiwait(handles.figure1);
% --- Outputs from this function are returned to the command line.
function varargout = morse_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;
function L_edit_Callback(hObject, eventdata, handles)
% hObject handle to L_edit (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 L_edit as text
% str2double(get(hObject,'String')) returns contents of L_edit as a double
Letter=get(hObject,'string');
L=str2double(Letter);
handles.L=L;
guidata(hObject,handles)
% --- Executes during object creation, after setting all properties.
function L_edit_CreateFcn(hObject, eventdata, handles)
% hObject handle to L_edit (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 PB_M.
function PB_M_Callback(hObject, eventdata, handles)
% hObject handle to PB_M (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
Get(handles.L,'string')
morse={'.----','..---','...--','....-','.....','-....','--...','---..','----.','-----','.-','-...','-.-.','-..','.','..-.','--.','....','..','.---','-.-','.-..','--','-.','---','.--.','--.-','.-.','...','-','..-','...-','.--','-..-','-.--','--..','/'};
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(Letter);
[~, index] = ismember(Letter(i), NumberOrLetter);
if index > 0
set(handle.Morse,sprintf,'string');
end
end
% --- Executes on button press in PB_L.
function PB_L_Callback(hObject, eventdata, handles)
% hObject handle to PB_L (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
Get(handles.M,'string')
morse={'.----','..---','...--','....-','.....','-....','--...','---..','----.','-----','.-','-...','-.-.','-..','.','..-.','--.','....','..','.---','-.-','.-..','--','-.','---','.--.','--.-','.-.','...','-','..-','...-','.--','-..-','-.--','--..','/'};
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(Morse);
[~, index] = ismember(m(i),NumberOrLetter);
if index > 0
set(handle.Morse,sprintf,'string');
end
end
function M_edit_Callback(hObject, eventdata, handles)
% hObject handle to M_edit (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 M_edit as text
% str2double(get(hObject,'String')) returns contents of M_edit as a double
Morse=get(hObject,'string');
M_num=str2double(Morse);
handles.M=M;
guidata(hObject,handles)
% --- Executes during object creation, after setting all properties.
function M_edit_CreateFcn(hObject, eventdata, handles)
% hObject handle to M_edit (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

Connectez-vous pour commenter.

 Réponse acceptée

Siti - in your PB_M_Callback callback (which presumably converts the alphanumeric text to morse code), the code is
% --- Executes on button press in PB_M.
function PB_M_Callback(hObject, eventdata, handles)
% hObject handle to PB_M (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
Letter=handles.Letter
morse={'.----','..---','...--','....-','.....','-....','--...','---..','----.','-----','.-','-...','-.-.','-..','.','..-.','--.','....','..','.---','-.-','.-..','--','-.','---','.--.','--.-','.-.','...','-','..-','...-','.--','-..-','-.--','--..','/'};
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(Letter);
[~, index] = ismember(Letter(i), NumberOrLetter);
if index > 0
set(handle.Morse,sprintf,'string');
end
end
I suspect that the handles.Letter is the handle to the edit text control that the user types the alphanumeric string into. Is this the case? If true, and you want to get the string you (may) need to get the string value as handles.Letter is a handle). Once found, you will want to iterate over each character in the string and convert it to the morse code equivalent. The code
set(handle.Morse,sprintf,'string');
is incorrect - you need to specify the 'String' property before the value you want to assign to it, and sprintf is a built-in MATLAB function so I'm not sure what your intent is here.
I think you could do something like
% --- Executes on button press in PB_M.
function PB_M_Callback(hObject, eventdata, handles)
% hObject handle to PB_M (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
word = char(get(handles.Letter, 'String'));
wordToMorseCode = '';
morse={'.----','..---','...--','....-','.....','-....','--...','---..','----.','-----','.-','-...','-.-.','-..','.','..-.','--.','....','..','.---','-.-','.-..','--','-.','---','.--.','--.-','.-.','...','-','..-','...-','.--','-..-','-.--','--..','/'};
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 k=1:length(word);
character = word(k);
[~, idx] = ismember(character, NumberOrLetter)
if ~isempty(idx)
wordToMorseCode = [wordToMorseCode ' ' morse(idx)];
end
end
set(handle.Morse, 'String', wordToMorseCode);
I've haven't tested the above but i think that you get the idea. I am assuming that word can be an array of one or more characters...if it is only one character, then the code should still work. I'm also assuming that there is a 1-1 mapping in the NumberOrLetter array to morse array. So we iterate over each character in the word and check to see if it is a member of NumberOrLetter. If it is, then we append the morse code equivalent to the wordToMorseCode string. Once we have iterasted over each character, then we set this string to the handle.Morse control.
The code will be similar when converting from morse code to the alphanumeric characters...but you will need to be a bit more aware that there are multiple characters in a single morse code that needs to get mapped to a single alphanumeric character.

15 commentaires

Now they say this
Error using handle.handle/get
Invalid or deleted object.
Error in morse>PB_M_Callback (line 107)
word = char(get(handles.L, 'String'));
Error in gui_mainfcn (line 95)
feval(varargin{:});
Error in morse (line 42)
gui_mainfcn(gui_State, varargin{:});
Error in
matlab.graphics.internal.figfile.FigFile/read>@(hObject,eventdata)morse('PB_M_Callback',hObject,eventdata,guidata(hObject))
Error while evaluating UIControl Callback.
Is L a valid handle to a edit text control? Why are you using L and not Letter? You may need to attach your figure to this question...
because L is my tag name for the edit text box. I thought you have to use the tag name for the handle
But was it originally Letter? Are you re-using existing code and then changing controls in the GUI (fig file)? Take a look at the handles structure (when you are running your GUI). What fields does it have?
Nope it is originally L
So what fields does handles have (when you run the code...)?
I'm sorry but I dont understand what you mean by fields.
and I tried putting on the code that you gave me
it says this
Error using get
Invalid handle
Error in morse>PB_M_Callback (line 87)
word = char(get(handles.Letter, 'String'));
Error in gui_mainfcn (line 95)
feval(varargin{:});
Error in morse (line 38)
gui_mainfcn(gui_State, varargin{:});
Error in
matlab.graphics.internal.figfile.FigFile/read>@(hObject,eventdata)morse('PB_M_Callback',hObject,eventdata,guidata(hObject))
Error while evaluating UIControl Callback.
Below is a new code:
function varargout = morse(varargin)
% MORSE MATLAB code for morse.fig
% MORSE, by itself, creates a new MORSE or raises the existing
% singleton*.
%
% H = MORSE returns the handle to a new MORSE or the handle to
% the existing singleton*.
%
% MORSE('CALLBACK',hObject,eventData,handles,...) calls the local
% function named CALLBACK in MORSE.M with the given input arguments.
%
% MORSE('Property','Value',...) creates a new MORSE or raises the
% existing singleton*. Starting from the left, property value pairs are
% applied to the GUI before morse_OpeningFcn gets called. An
% unrecognized property name or invalid value makes property application
% stop. All inputs are passed to morse_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 morse
% Last Modified by GUIDE v2.5 07-Apr-2020 14:41:41
% Begin initialization code - DO NOT EDIT
gui_Singleton = 1;
gui_State = struct('gui_Name', mfilename, ...
'gui_Singleton', gui_Singleton, ...
'gui_OpeningFcn', @morse_OpeningFcn, ...
'gui_OutputFcn', @morse_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 morse is made visible.
function morse_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 morse (see VARARGIN)
% Choose default command line output for morse
handles.output = hObject;
% Update handles structure
guidata(hObject, handles);
% UIWAIT makes morse wait for user response (see UIRESUME)
% uiwait(handles.figure1);
% --- Outputs from this function are returned to the command line.
function varargout = morse_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;
function L_edit_Callback(hObject, eventdata, handles)
% hObject handle to L_edit (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 L_edit as text
% str2double(get(hObject,'String')) returns contents of L_edit as a double
letter=get(hObject,'string');
Letter_num=str2double(letter);
handles.Letter=letter;
guidata(hObject,handles)
% --- Executes during object creation, after setting all properties.
function L_edit_CreateFcn(hObject, eventdata, handles)
% hObject handle to L_edit (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 PB_M.
function PB_M_Callback(hObject, eventdata, handles)
% hObject handle to PB_M (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
word = char(get(handles.Letter, 'String'));
wordToMorseCode = '';
morse={'.----','..---','...--','....-','.....','-....','--...','---..','----.','-----','.-','-...','-.-.','-..','.','..-.','--.','....','..','.---','-.-','.-..','--','-.','---','.--.','--.-','.-.','...','-','..-','...-','.--','-..-','-.--','--..','/'};
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 k=1:length(word);
character = word(k);
[~, idx] = ismember(character, NumberOrLetter)
if ~isempty(idx)
wordToMorseCode = [wordToMorseCode ' ' morse(idx)];
end
end
set(handle.Morse, 'String', wordToMorseCode);
% --- Executes on button press in PB_L.
function PB_L_Callback(hObject, eventdata, handles)
% hObject handle to PB_L (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
Morse=handles.Morse
morse={'.----','..---','...--','....-','.....','-....','--...','---..','----.','-----','.-','-...','-.-.','-..','.','..-.','--.','....','..','.---','-.-','.-..','--','-.','---','.--.','--.-','.-.','...','-','..-','...-','.--','-..-','-.--','--..','/'};
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(Morse);
[~, index] = ismember(morse(i),NumberOrLetter);
if index > 0
set(handle.Morse,sprintf,'string');
end
end
function M_edit_Callback(hObject, eventdata, handles)
% hObject handle to M_edit (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 M_edit as text
% str2double(get(hObject,'String')) returns contents of M_edit as a double
Morse=get(hObject,'string');
Morse_num=str2double(Morse);
handles.Morse=Morse;
guidata(hObject,handles)
% --- Executes during object creation, after setting all properties.
function M_edit_CreateFcn(hObject, eventdata, handles)
% hObject handle to M_edit (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
handles is a structure that has fields for each of the controls that you define in your GUI.
function PB_M_Callback(hObject, eventdata, handles)
% hObject handle to PB_M (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
word = char(get(handles.Letter, 'String'));
In this piece of code, Letter is considered to be a field and you are accessing it with the '.'. I don't know why you are now getting an error that states "invalid handle" unless of course you have somehow overwritten this field earlier in your code with something else....which is exactly what you are doing here
function L_edit_Callback(hObject, eventdata, handles)
% hObject handle to L_edit (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 L_edit as text
% str2double(get(hObject,'String')) returns contents of L_edit as a double
letter=get(hObject,'string');
Letter_num=str2double(letter);
handles.Letter=letter;
guidata(hObject,handles)
You are replacing the handle to the Letter edit control with a double (why the conversion to a double with str2double?) and so the error message makes sense. Just remove all of the code in this callback (you don't need to do any of the above four lines of code). Then the code in your button callback should work.
Do the same thing in M_edit_Callback. Remove all of your code here because you are corrupting the handle to Morse.
did all that but now it says this is the error:
Reference to non-existent field 'Letter'.
Error in Morse2>PB_M_Callback (line 104)
word = char(get(handles.Letter, 'String'));
Error in gui_mainfcn (line 95)
feval(varargin{:});
Error in Morse2 (line 42)
gui_mainfcn(gui_State, varargin{:});
Error in
matlab.graphics.internal.figfile.FigFile/read>@(hObject,eventdata)Morse2('PB_M_Callback',hObject,eventdata,guidata(hObject))
Error while evaluating UIControl Callback.
function varargout = Morse2(varargin)
% MORSE2 MATLAB code for Morse2.fig
% MORSE2, by itself, creates a new MORSE2 or raises the existing
% singleton*.
%
% H = MORSE2 returns the handle to a new MORSE2 or the handle to
% the existing singleton*.
%
% MORSE2('CALLBACK',hObject,eventData,handles,...) calls the local
% function named CALLBACK in MORSE2.M with the given input arguments.
%
% MORSE2('Property','Value',...) creates a new MORSE2 or raises the
% existing singleton*. Starting from the left, property value pairs are
% applied to the GUI before Morse2_OpeningFcn gets called. An
% unrecognized property name or invalid value makes property application
% stop. All inputs are passed to Morse2_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 Morse2
% Last Modified by GUIDE v2.5 08-Apr-2020 10:08:33
% Begin initialization code - DO NOT EDIT
gui_Singleton = 1;
gui_State = struct('gui_Name', mfilename, ...
'gui_Singleton', gui_Singleton, ...
'gui_OpeningFcn', @Morse2_OpeningFcn, ...
'gui_OutputFcn', @Morse2_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 Morse2 is made visible.
function Morse2_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 Morse2 (see VARARGIN)
% Choose default command line output for Morse2
handles.output = hObject;
% Update handles structure
guidata(hObject, handles);
% UIWAIT makes Morse2 wait for user response (see UIRESUME)
% uiwait(handles.figure1);
% --- Outputs from this function are returned to the command line.
function varargout = Morse2_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;
function L_edit_Callback(hObject, eventdata, handles)
% hObject handle to L_edit (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 L_edit as text
% str2double(get(hObject,'String')) returns contents of L_edit as a double
% --- Executes during object creation, after setting all properties.
function L_edit_CreateFcn(hObject, eventdata, handles)
% hObject handle to L_edit (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 PB_M.
function PB_M_Callback(hObject, eventdata, handles)
% hObject handle to PB_M (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
word = char(get(handles.Letter, 'String'));
wordToMorseCode = '';
morse={'.----','..---','...--','....-','.....','-....','--...','---..','----.','-----','.-','-...','-.-.','-..','.','..-.','--.','....','..','.---','-.-','.-..','--','-.','---','.--.','--.-','.-.','...','-','..-','...-','.--','-..-','-.--','--..','/'};
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 k=1:length(word);
character = word(k);
[~, idx] = ismember(character, NumberOrLetter)
if ~isempty(idx)
wordToMorseCode = [wordToMorseCode ' ' morse(idx)];
end
end
set(handle.Morse, 'String', wordToMorseCode);
% --- Executes on button press in PB_L.
function PB_L_Callback(hObject, eventdata, handles)
% hObject handle to PB_L (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
word = char(get(handles.Morse, 'String'));
wordToMorseCode = '';
morse={'.----','..---','...--','....-','.....','-....','--...','---..','----.','-----','.-','-...','-.-.','-..','.','..-.','--.','....','..','.---','-.-','.-..','--','-.','---','.--.','--.-','.-.','...','-','..-','...-','.--','-..-','-.--','--..','/'};
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 k=1:length(word);
character = word(k);
[~, idx] = ismember(character, NumberOrLetter)
if ~isempty(idx)
wordToMorseCode = [wordToMorseCode ' ' NumberOrLetter(idx)];
end
end
set(handle.Letter, 'String', wordToMorseCode);
function M_edit_Callback(hObject, eventdata, handles)
% hObject handle to M_edit (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 M_edit as text
% str2double(get(hObject,'String')) returns contents of M_edit as a double
% --- Executes during object creation, after setting all properties.
function M_edit_CreateFcn(hObject, eventdata, handles)
% hObject handle to M_edit (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
Ok...but what fields exist in the handles structure? It looks like sometimes Letter is a valid field and other times L is a valid field. Looking at the code abuve, there is
% --- Executes during object creation, after setting all properties.
function L_edit_CreateFcn(hObject, eventdata, handles)
% hObject handle to L_edit (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.
which suggests a edit control named L....so instead of using handles.Letter, use handles.L. That is why I asked you to check the fields of handles...so you know exactly what control to reference.
alright sorry for that L_edit is the field. But right now it managed to work but it is not displaying thw morse code instead it gave me this. and now my gui instead of having 2 text box it only has one now
>> Morse2
idx =
23
Warning: Single line Edit Controls can not have multi-line text
function varargout = Morse2(varargin)
% MORSE2 MATLAB code for Morse2.fig
% MORSE2, by itself, creates a new MORSE2 or raises the existing
% singleton*.
%
% H = MORSE2 returns the handle to a new MORSE2 or the handle to
% the existing singleton*.
%
% MORSE2('CALLBACK',hObject,eventData,handles,...) calls the local
% function named CALLBACK in MORSE2.M with the given input arguments.
%
% MORSE2('Property','Value',...) creates a new MORSE2 or raises the
% existing singleton*. Starting from the left, property value pairs are
% applied to the GUI before Morse2_OpeningFcn gets called. An
% unrecognized property name or invalid value makes property application
% stop. All inputs are passed to Morse2_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 Morse2
% Last Modified by GUIDE v2.5 08-Apr-2020 10:08:33
% Begin initialization code - DO NOT EDIT
gui_Singleton = 1;
gui_State = struct('gui_Name', mfilename, ...
'gui_Singleton', gui_Singleton, ...
'gui_OpeningFcn', @Morse2_OpeningFcn, ...
'gui_OutputFcn', @Morse2_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 Morse2 is made visible.
function Morse2_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 Morse2 (see VARARGIN)
% Choose default command line output for Morse2
handles.output = hObject;
% Update handles structure
guidata(hObject, handles);
% UIWAIT makes Morse2 wait for user response (see UIRESUME)
% uiwait(handles.figure1);
% --- Outputs from this function are returned to the command line.
function varargout = Morse2_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;
function L_edit_Callback(hObject, eventdata, handles)
% hObject handle to L_edit (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 L_edit as text
% str2double(get(hObject,'String')) returns contents of L_edit as a double
% --- Executes during object creation, after setting all properties.
function L_edit_CreateFcn(hObject, eventdata, handles)
% hObject handle to L_edit (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 PB_M.
function PB_M_Callback(hObject, eventdata, handles)
% hObject handle to PB_M (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
word = char(get(handles.L_edit, 'String'));
wordToMorseCode = '';
morse={'.----','..---','...--','....-','.....','-....','--...','---..','----.','-----','.-','-...','-.-.','-..','.','..-.','--.','....','..','.---','-.-','.-..','--','-.','---','.--.','--.-','.-.','...','-','..-','...-','.--','-..-','-.--','--..','/'};
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 k=1:length(word);
character = word(k);
[~, idx] = ismember(character, NumberOrLetter)
if ~isempty(idx)
wordToMorseCode = [wordToMorseCode ' ' morse(idx)];
end
end
set(handles.M_edit, 'String', wordToMorseCode);
% --- Executes on button press in PB_L.
function PB_L_Callback(hObject, eventdata, handles)
% hObject handle to PB_L (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
word = char(get(handles.M_edit, 'String'));
wordToMorseCode = '';
morse={'.----','..---','...--','....-','.....','-....','--...','---..','----.','-----','.-','-...','-.-.','-..','.','..-.','--.','....','..','.---','-.-','.-..','--','-.','---','.--.','--.-','.-.','...','-','..-','...-','.--','-..-','-.--','--..','/'};
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 k=1:length(word);
character = word(k);
[~, idx] = ismember(character, NumberOrLetter)
if ~isempty(idx)
wordToMorseCode = [wordToMorseCode ' ' NumberOrLetter(idx)];
end
end
set(handles.L_edit, 'String', wordToMorseCode);
function M_edit_Callback(hObject, eventdata, handles)
% hObject handle to M_edit (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 M_edit as text
% str2double(get(hObject,'String')) returns contents of M_edit as a double
% --- Executes during object creation, after setting all properties.
function M_edit_CreateFcn(hObject, eventdata, handles)
% hObject handle to M_edit (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
Pardon for asking too many questions. Right now I think the numbers on the command windows means the position of the Morse code. But the morse is not appearing on my text box for morse.
idx =
23
idx =
25
idx =
25
that is what they give me on the GUI when i type moo. But the morse code is not appearing on the gui text box that i made
yes, the idx from above corresponds to the line of code
[~, idx] = ismember(character, NumberOrLetter)
because this line does not terminate with a semi-colon. If we write as
[~, idx] = ismember(character, NumberOrLetter);
then the output to the command window will be suppressed.
I see thst morse is a cell array, so maybe try changing the code to
for k=1:length(word);
character = word(k);
[~, idx] = ismember(character, NumberOrLetter)
if ~isempty(idx)
if isempty(wordToMorseCode)
wordToMorseCode = morse{idx};
else
wordToMorseCode = [wordToMorseCode ' ' morse{idx}];
end
end
end
The change is using {} braces instead of () for indexing into this cell array. I've confirmed that if word is 'MOO' then wordToMorseCode is '-- --- ---'.
Thank you so much it worked perfectly

Connectez-vous pour commenter.

Plus de réponses (0)

Catégories

Produits

Tags

Community Treasure Hunt

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

Start Hunting!

Translated by