Equation is working but the output is not displaying

6 views (last 30 days)
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 Comments
Voss
Voss on 13 Apr 2022
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'

Sign in to comment.

Accepted Answer

Geoff Hayes
Geoff Hayes on 11 Apr 2022
@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 Comments

Sign in to comment.

More Answers (0)

Categories

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

Tags

Community Treasure Hunt

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

Start Hunting!