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

4 views (last 30 days)
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 Comments
Geoff Hayes
Geoff Hayes on 7 Apr 2020
Siti - please copy and paste the full error message to this question. Do you have the figure/GUI or just the above code?
Siti Khadijah Norzafri
Siti Khadijah Norzafri on 7 Apr 2020
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

Sign in to comment.

Accepted Answer

Geoff Hayes
Geoff Hayes on 7 Apr 2020
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 Comments
Geoff Hayes
Geoff Hayes on 8 Apr 2020
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 '-- --- ---'.

Sign in to comment.

More Answers (0)

Categories

Find more on Migrate GUIDE Apps in Help Center and File Exchange

Tags

Products

Community Treasure Hunt

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

Start Hunting!