Equation is working but the output is not displaying

조회 수: 7 (최근 30일)
Nicholas Mun
Nicholas Mun 2022년 4월 11일
댓글: Voss 2022년 4월 13일
I have created a program to automatically convert letters into morse code, but for some reason the output is not being saved, can someone please help me on this? thanks
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 11-Apr-2022 19:19:26
% 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 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)
userinput = handles.text_input;
output_string = '';
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
output_string = [output_string ' ' morsecode(index)];
end
end
fprintf('\n');
set(handles.morse, 'String' , output_string);
% --- Executes on button press in pushbutton3.
function pushbutton3_Callback(hObject, eventdata, handles)
% hObject handle to pushbutton3 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
  댓글 수: 3
Nicholas Mun
Nicholas Mun 2022년 4월 13일
thanks! but when i try to use the third way you suggested (the get() function) i get an error saying that my tag is invalid, here is the exact error
Error using get
Invalid handle
Error in FOPfinal>pushbutton2_Callback (line 117)
userinput = get(handles.text_input);
Error in gui_mainfcn (line 95)
feval(varargin{:});
Error in FOPfinal (line 42)
gui_mainfcn(gui_State, varargin{:});
Error in matlab.graphics.internal.figfile.FigFile/read>@(hObject,eventdata)FOPfinal('pushbutton2_Callback',hObject,eventdata,guidata(hObject))
But in my guide editor the tag text_input does exist for one of the edit boxes, and i dont know why the error occurs. Here is a picture of the inpector of that editbox
Voss
Voss 2022년 4월 13일
This is caused by the very problem I was explaining ... overwriting handles.text_input with something that's not a handle. I would restart the GUI and try it again, after making this change:
function pushbutton2_Callback(hObject, eventdata, handles)
userinput = get(handles.text_input,'String'); % don't forget the 'String'

댓글을 달려면 로그인하십시오.

채택된 답변

Geoff Hayes
Geoff Hayes 2022년 4월 11일
@Nicholas Mun - without knowing exactly what the input string looks like, I think that you need to replace
output_string = [output_string ' ' morsecode(index)];
with
output_string = [output_string ' ' morsecode{index}];
. Since morsecode is a cell array of strings, then we use the {} to index into the array to extract the string. Note that when you use (), a cell is returned instead (which means that the output_string would be cell array.
Note that you should probably remove the line
fprintf('\n');
as I don't think it serves any real purpose.
  댓글 수: 3
Geoff Hayes
Geoff Hayes 2022년 4월 12일
@Nicholas Mun - if you use the MATLAB debugger you can step through the code and see that the array updated with
moutput = [moutput morsecode(index)];
is a cell array rather than a string. I think that you should revert the above to
moutput = [moutput ' ' morsecode{index}];
Note how we use the {} rather than () when indexing into morsecode.
Nicholas Mun
Nicholas Mun 2022년 4월 12일
works perfectly now thanks!

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Migrate GUIDE Apps에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by