hi can anyone tell me what is wrong with my morse code encoder and decoder gui program
1 次查看(过去 30 天)
显示 更早的评论
whenever i run it this appears
Dot indexing is not supported for variables of this type.
Error in morsegui>edit1_CreateFcn (line 104)
input=char(get(handles.edit1,'string'));
Error in gui_mainfcn (line 95)
feval(varargin{:});
Error in morsegui (line 42)
gui_mainfcn(gui_State, varargin{:});
Error in matlab.graphics.internal.figfile.FigFile/read>@(hObject,eventdata)morsegui('edit1_CreateFcn',hObject,eventdata,guidata(hObject
Here's my code
function varargout = morsegui(varargin)
% MORSEGUI MATLAB code for morsegui.fig
% MORSEGUI, by itself, creates a new MORSEGUI or raises the existing
% singleton*.
%
% H = MORSEGUI returns the handle to a new MORSEGUI or the handle to
% the existing singleton*.
%
% MORSEGUI('CALLBACK',hObject,eventData,handles,...) calls the local
% function named CALLBACK in MORSEGUI.M with the given input arguments.
%
% MORSEGUI('Property','Value',...) creates a new MORSEGUI or raises the
% existing singleton*. Starting from the left, property value pairs are
% applied to the GUI before morsegui_OpeningFcn gets called. An
% unrecognized property name or invalid value makes property application
% stop. All inputs are passed to morsegui_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 morsegui
% Last Modified by GUIDE v2.5 16-Apr-2022 06:00:45
% Begin initialization code - DO NOT EDIT
gui_Singleton = 1;
gui_State = struct('gui_Name', mfilename, ...
'gui_Singleton', gui_Singleton, ...
'gui_OpeningFcn', @morsegui_OpeningFcn, ...
'gui_OutputFcn', @morsegui_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 morsegui is made visible.
function morsegui_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 morsegui (see VARARGIN)
% Choose default command line output for morsegui
handles.output = hObject;
% Update handles structure
guidata(hObject, handles);
% UIWAIT makes morsegui wait for user response (see UIRESUME)
% uiwait(handles.figure1);
% --- Outputs from this function are returned to the command line.
function varargout = morsegui_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)
% 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 edit1_Callback(hObject, eventdata, handles)
% 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
% --- Executes during object creation, after setting all properties.
function edit1_CreateFcn(hObject, eventdata, handles)
% 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
input=char(get(handles.edit1,'string'));
input=upper(input);
word_to_morse= '';
morse={'.-','-...','-.-.','-..','.','..-.','--.','....','..','.---','-.-','.-..','--','-.','---','.--.','--.-','.-.','...','-','..-','...-','.--','-..-','-.--','--..','/'};
letters={'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(input)
character = input(i);
[~, index] = ismember(character, letters);
if ~isempty(index)
if isempty(word_to_morse)
word_to_morse = morse{index};
else
word_to_morse = [word_to_morse ' ' morse{index}];
end
end
end
set(handles.text2,'string',word_to_morse);
input=char(get(handles.edit1,'string'));
input=upper(input);
morse={'.-','-...','-.-.','-..','.','..-.','--.','....','..','.---','-.-','.-..','--','-.','---','.--.','--.-','.-.','...','-','..-','...-','.--','-..-','-.--','--..','/'};
letters={'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',' '};
text=strsplit(input,' ');
text(cellfun(@isempty, text)) = [];
[~, index] = ismember(text, morse);
translated=(letters(index));
morse_to_word=strjoin(translated, '');
set(handles.text2,'string',morse_to_word);
1 个评论
Walter Roberson
2022-4-15
[~, index] = ismember(character, letters);
if ~isempty(index)
When the first parameter to ismember() is not found in the second parameter, then the second output is not empty. Instead it is a vector the same length as the first parameter, in which the value of the vector at the location is 0.
For example,
[~, index] = ismember({'A', 'C'}, {'B', 'C'})
the first input value, 'A', is not a member of 'B' or 'C' so the output for that case will be 0. The second input value, 'C' is a member of 'C' so the second output for that case will be the index into the second parameter, which would be 2 in this case. The overall result in index would be 0, 2 not emptiness and 2
回答(2 个)
Voss
2022-4-15
It looks like you've accidentally put the code you wanted to be in edit1_Callback into edit1_CreateFcn instead.
Move the code from edit1_CreateFcn to edit1_Callback, so that those two functions look like this:
%
% (other functions above removed for clarity)
%
%
function edit1_Callback(hObject, eventdata, handles)
% 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
% --- Executes during object creation, after setting all properties.
input=char(get(handles.edit1,'string'));
input=upper(input);
word_to_morse= '';
morse={'.-','-...','-.-.','-..','.','..-.','--.','....','..','.---','-.-','.-..','--','-.','---','.--.','--.-','.-.','...','-','..-','...-','.--','-..-','-.--','--..','/'};
letters={'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(input)
character = input(i);
[~, index] = ismember(character, letters);
if ~isempty(index)
if isempty(word_to_morse)
word_to_morse = morse{index};
else
word_to_morse = [word_to_morse ' ' morse{index}];
end
end
end
set(handles.text2,'string',word_to_morse);
input=char(get(handles.edit1,'string'));
input=upper(input);
morse={'.-','-...','-.-.','-..','.','..-.','--.','....','..','.---','-.-','.-..','--','-.','---','.--.','--.-','.-.','...','-','..-','...-','.--','-..-','-.--','--..','/'};
letters={'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',' '};
text=strsplit(input,' ');
text(cellfun(@isempty, text)) = [];
[~, index] = ismember(text, morse);
translated=(letters(index));
morse_to_word=strjoin(translated, '');
set(handles.text2,'string',morse_to_word);
function edit1_CreateFcn(hObject, eventdata, handles)
% 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
That should help you avoid that particular error.
6 个评论
Walter Roberson
2022-4-15
Read the comments in your code:
function edit1_CreateFcn(hObject, eventdata, handles)
% 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
^^^^^ ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
Do not put the work into a CreateFcn callback: those should only do things like configure colors or position, and then should return. The handles structure does not exist until after all of the CreateFcn have been executed.
You should be creating two buttons, one of which has a callback that converts words to morse, and the other of which has a callback that converts morse to words.
Or, instead of using two buttons, you could have two uicontrol 'edit' fields, and configure their Callback properties, one to convert words to morse, and the other to convert morse to words.
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Graphics Object Properties 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!