I can't seem to generate a Morse code output on my edit2 static text in my GUI, is there something wrong with my code? We're supposed to create a GUI which converts Letter/Numbers into Morse code..

1 visualización (últimos 30 días)
function varargout = Morsee(varargin)
% MORSEE MATLAB code for Morsee.fig
% MORSEE, by itself, creates a new MORSEE or raises the existing
% singleton*.
%
% H = MORSEE returns the handle to a new MORSEE or the handle to
% the existing singleton*.
%
% MORSEE('CALLBACK',hObject,eventData,handles,...) calls the local
% function named CALLBACK in MORSEE.M with the given input arguments.
%
% MORSEE('Property','Value',...) creates a new MORSEE or raises the
% existing singleton*. Starting from the left, property value pairs are
% applied to the GUI before Morsee_OpeningFcn gets called. An
% unrecognized property name or invalid value makes property application
% stop. All inputs are passed to Morsee_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 Morsee
% Last Modified by GUIDE v2.5 21-Apr-2021 00:46:25
% Begin initialization code - DO NOT EDIT
gui_Singleton = 1;
gui_State = struct('gui_Name', mfilename, ...
'gui_Singleton', gui_Singleton, ...
'gui_OpeningFcn', @Morsee_OpeningFcn, ...
'gui_OutputFcn', @Morsee_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 Morsee is made visible.
function Morsee_OpeningFcn(hObject, ~, 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 Morsee (see VARARGIN)
% Choose default command line output for Morsee
handles.output = hObject;
% Update handles structure
guidata(hObject, handles);
% UIWAIT makes Morsee wait for user response (see UIRESUME)
% uiwait(handles.figure1);
% --- Outputs from this function are returned to the command line.
function varargout = Morsee_OutputFcn(~, ~, 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 edit1_Callback(~, ~, ~)
% hObject handle to edit1 (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 edit1 as text
% str2double(get(hObject,'String')) returns contents of edit1 as a double
edit1=get(hObject,'String');
edit1_num=str2double(edit1);
handles.edit1_num=edit1_num;
guidata(hObject,handles);
% --- Executes during object creation, after setting all properties.
function edit1_CreateFcn(~, ~, ~)
% hObject handle to edit1 (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 edit2_Callback(~, ~, ~)
% hObject handle to edit2 (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 edit2 as text
% str2double(get(hObject,'String')) returns contents of edit2 as a double
set(handles.edit2,'string',morse(idx))
% --- Executes during object creation, after setting all properties.
function edit2_CreateFcn(hObject, ~, ~)
% hObject handle to edit2 (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 pushbutton1.
function pushbutton1_Callback(~, ~, handles)
% 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)
get(handles.edit1_num,'String');
x = char(get(handles.edit1_num,'String'));
morse='';
code = {'.-';'-...';'-.-.';'-..';'.';'..-.';'--.';'....';'..';'.---'; ...
'-.-';'.-..';'--';'-.';'---';'.--.';'--.-';'.-.'; ...
'...';'-';'..-';'...-';'.--';'-..-';'-.--';'--..'; ...
'.----';'..---';'...--';'....-';'.....';'-....'; ...
'--...';'---..'; '----.';'-----';'_'};
chars = {'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(x)
if ~isempty(x)
[~, x] = ismember(code(x),chars);
morse(i) = [morse ' ' code(x)];
end
end
set(handles.edit2,'String',morse(i))
set(handles.edit2,'String',edit1)

Respuestas (1)

Walter Roberson
Walter Roberson el 21 de Abr. de 2021
x = char(get(handles.edit1_num,'String'));
Okay, x is what the user input.
for i=1:length(x)
You want to proceed through the input one character at a time.
if ~isempty(x)
If x was empty then when you did length(x) that would have been 0 and you would not have entered the for loop. This if is strange... unless you are modifying x for some reason.
[~, x] = ismember(code(x),chars);
You use the characters of the user input as indexes into the cell code . The letters will typically be the letters and numbers, so for example '2 cents' . Characters as indexes are converted to the unicode code point, so for example '2' is character position 50 and space is character position 32, and c is character position 99, so '2 c' would be indexing at [50 32 99]. But your code vector is not that large, and you would get index out of range.
If you fixed that so you did not get index out of range, then code(x) would be selected cell array entries, such as {'-...', '_', '--'} and you would compare those to the individual characters such as '2' and ' ' and 'c' . But none of those are going to match, since they consist of dots and dashes and none of the entries in chars have those.
Also be careful, the input might be lower-case but your letters array only has upper case.
So all the matches will fail, so they will all generate index 0. But suppose you fixed that in the future, so suppose the indices returned into the second output were a mix of positive integers and 0. That vector would have length equal to the number of elements in code(x) which would be the same as the length of x. You then take that vector of indices, and use it to overwrite all of x -- the x that contained the letters the user input.
morse(i) = [morse ' ' code(x)];
but x here is a different x than the input x -- it is now the indices instead of the characters. You would have had to ensure that the indices are the same as the unicode positions in order for that code(x) on this line to refer to the same locations as the code(x) on the ismember(). And if you know for sure that those will be the same, what is the point in going through the ismember() ??
code is a cell array of morse code. When you index a cell array with () you get back a cell array. You are using [] between a character vector and a cell array. Fortunately for you that is defined -- the result would be the same as
morse(i) = [morse, {' '}, code(x)];
but the right hand side is a cell array of minimum length 2, and you are trying to store that non-scalar cell array into the single location morse(i)
And then you loop back in the for loop because... Uh? Why??
  6 comentarios
Anonymous kitten
Anonymous kitten el 21 de Abr. de 2021
Editada: Anonymous kitten el 21 de Abr. de 2021
to morse, actually i'm still quite unsure on how to use this index thing as I just started learning Matlab, apologies... But how should I apply it in this case?

Iniciar sesión para comentar.

Categorías

Más información sobre Interactive Control and Callbacks en Help Center y File Exchange.

Community Treasure Hunt

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

Start Hunting!

Translated by