FIC MATLAB 2018-1/Aula-17

De MediaWiki do Campus São José
Revisão de 22h07min de 10 de maio de 2018 por Roberto.nobrega (discussão | contribs)
(dif) ← Edição anterior | Revisão atual (dif) | Versão posterior → (dif)
Ir para navegação Ir para pesquisar
  • Código da interface (interface1.m):
function varargout = interface1(varargin)
% INTERFACE1 MATLAB code for interface1.fig
%      INTERFACE1, by itself, creates a new INTERFACE1 or raises the existing
%      singleton*.
%
%      H = INTERFACE1 returns the handle to a new INTERFACE1 or the handle to
%      the existing singleton*.
%
%      INTERFACE1('CALLBACK',hObject,eventData,handles,...) calls the local
%      function named CALLBACK in INTERFACE1.M with the given input arguments.
%
%      INTERFACE1('Property','Value',...) creates a new INTERFACE1 or raises the
%      existing singleton*.  Starting from the left, property value pairs are
%      applied to the GUI before interface1_OpeningFcn gets called.  An
%      unrecognized property name or invalid value makes property application
%      stop.  All inputs are passed to interface1_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 interface1

% Last Modified by GUIDE v2.5 10-May-2018 19:26:51

% Begin initialization code - DO NOT EDIT
gui_Singleton = 1;
gui_State = struct('gui_Name',       mfilename, ...
                   'gui_Singleton',  gui_Singleton, ...
                   'gui_OpeningFcn', @interface1_OpeningFcn, ...
                   'gui_OutputFcn',  @interface1_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 interface1 is made visible.
function interface1_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 interface1 (see VARARGIN)

% Choose default command line output for interface1
handles.output = hObject;

% Criação de variáveis
handles.contador = 0;
handles.frequencia = 0;
handles.selecao = 'cosseno';

% Marcando a seleção do cosseno como padrão
handles.radiobutton1.Value = 1;
handles.radiobutton2.Value = 0;

% Update handles structure
guidata(hObject, handles);

% UIWAIT makes interface1 wait for user response (see UIRESUME)
% uiwait(handles.figure1);


% --- Outputs from this function are returned to the command line.
function varargout = interface1_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)
%disp('Desejo que sua noite seja de muita paz e harmonia...')
fs = 10000;
ts = 1/fs;
f = handles.frequencia;
n = 0:1000;

plot(handles.axes1,n*ts,sin(2*pi*f*n*ts),'b')

switch handles.selecao
    case 'cosseno'
        plot(handles.axes2,n*ts,cos(2*pi*f*n*ts),'r')
    case 'tangente'
        plot(handles.axes2,n*ts,tan(2*pi*f*n*ts),'g')
        ylim([-8 8])
end

handles.contador = handles.contador+1;

disp(['O botão foi pressionado ' num2str(handles.contador) ' vez', (handles.contador~=1)*'es'])
disp('A frequência é: ')
handles.frequencia

disp('Está selecionado o botão')
handles.selecao

% Update handles structure
guidata(hObject, handles);

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

if isnan(str2double(handles.edit1.String))
    errordlg('Digite um número, mané','Erro')
    fs = 100000;
    ts = 1/fs;
    t = 0:ts:1;
    sinal = (square(2*pi*202*t) + 0.5*square(2*pi*302*t) + 0.25*square(2*pi*402*t)).*exp(-t/0.25);
    sinal2 = [sinal sinal(1:2:end) sinal(1:4:end)];
    sound(sinal2,fs)
    handles.frequencia = 0;
else
    handles.frequencia = str2double(handles.edit1.String);
end

% Update handles structure
guidata(hObject, handles);

% --- 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

set(hObject,'BackgroundColor','yellow');

% 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','red');
end


% --- Executes on button press in radiobutton1.
function radiobutton1_Callback(hObject, eventdata, handles)
% hObject    handle to radiobutton1 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Hint: get(hObject,'Value') returns toggle state of radiobutton1
handles.radiobutton2.Value = 0;
handles.selecao = 'cosseno';

% Update handles structure
guidata(hObject, handles);

% --- Executes on button press in radiobutton2.
function radiobutton2_Callback(hObject, eventdata, handles)
% hObject    handle to radiobutton2 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Hint: get(hObject,'Value') returns toggle state of radiobutton2
handles.radiobutton1.Value = 0;
handles.selecao = 'tangente';

% Update handles structure
guidata(hObject, handles);