Equation is working but the output is not displaying
Ältere Kommentare anzeigen
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 Kommentare
Voss
am 12 Apr. 2022
@Nicholas Mun If you have an object stored in your handles structure called "text_input", e.g., an edit box where the user enters the string to be encoded, then you don't want to be doing this:
function text_input_Callback(hObject, eventdata, handles)
text_input=get(hObject,'string');
handles.text_input=text_input;
guidata(hObject,handles);
because these two lines:
handles.text_input=text_input;
guidata(hObject,handles);
overwite the value of handles.text_input, and set it to be the 'String' of the uicontrol that was previously known as handles.text_input.
So then any part of the code that expects handles.text_input to be an edit box (or whatever type of uicontrol) will have a problem because handles.text_input is now a character vector (or a string or a cell array - but not a uicontrol).
In your code, as it's shown here, that's not a problem because nowhere in the code refers to handles.text_input expecting it to be a uicontrol. However, there is the potential for the opposite problem: parts of the code expect handles.input_text to be a string (or whatever) when it is actually a uicontrol. Consider what happens if pushbutton2_Callback is executed before text_input_Callback is ever executed (i.e., the user clicks pushbutton2 without having entered text into text_input):
function pushbutton2_Callback(hObject, eventdata, handles)
% handles.text_input here is expected to be the string entered by the user,
% but if text_input_Callback has not been executed yet, then it is still the
% uicontrol handles.text_input ...
userinput = handles.text_input;
output_string = '';
userinput = upper(userinput);
% ... so you'll get an error here, trying to strsplit a uicontrol:
userinput = strjoin(strsplit(userinput));
In order to avoid this problem, you can still store the string the user entered, but don't use a name that's used by a uicontrol you might still need to access. For instance, you might call it text_input_by_user:
function text_input_Callback(hObject, eventdata, handles)
text_input=get(hObject,'string');
handles.text_input_by_user=text_input;
guidata(hObject,handles);
and then in pushbutton2_Callback obviously you would refer to that new name:
function pushbutton2_Callback(hObject, eventdata, handles)
userinput = handles.text_input_by_user;
However, you don't need to store it at all; you can always just get it from the uicontrol itself whenever you need it:
function pushbutton2_Callback(hObject, eventdata, handles)
userinput = get(handles.text_input,'String');
and the uicontrol handles.text_input doesn't need to have a Callback in that case.
Nicholas Mun
am 13 Apr. 2022
Voss
am 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'
Akzeptierte Antwort
Weitere Antworten (0)
Kategorien
Mehr zu Matrix Indexing finden Sie in Hilfe-Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!

