zoukankan      html  css  js  c++  java
  • MATLAB界面编程

    MATLAB界面编程

    Reference to non-exiting field!

    控件没有添加Tag属性:

    直接双击fig文件启动(会缺少初始化操作)和在m文件执行开始启动结果不一样:

    编译fig程序为EXE文件可同时执行多个实例:

    选择APP里面的Compiler:

    选择自己的工程并build,最后会在对应的文件目录下会生出EXE文件:

    设置背景颜色为白,方便截图(另存为emf再粘贴到word比较清晰)

     

    附件:

     实现FFT的MATLAB图形界面:

    function varargout = easy_fft_1(varargin)
    % EASY_FFT_1 MATLAB code for easy_fft_1.fig
    %      EASY_FFT_1, by itself, creates a new EASY_FFT_1 or raises the existing
    %      singleton*.
    %
    %      H = EASY_FFT_1 returns the handle to a new EASY_FFT_1 or the handle to
    %      the existing singleton*.
    %
    %      EASY_FFT_1('CALLBACK',hObject,eventData,handles,...) calls the local
    %      function named CALLBACK in EASY_FFT_1.M with the given input arguments.
    %
    %      EASY_FFT_1('Property','Value',...) creates a new EASY_FFT_1 or raises the
    %      existing singleton*.  Starting from the left, property value pairs are
    %      applied to the GUI before easy_fft_1_OpeningFcn gets called.  An
    %      unrecognized property name or invalid value makes property application
    %      stop.  All inputs are passed to easy_fft_1_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 easy_fft_1
    
    % Last Modified by GUIDE v2.5 10-Jul-2018 12:27:54
    
    % Begin initialization code - DO NOT EDIT
    gui_Singleton = 1;
    gui_State = struct('gui_Name',       mfilename, ...
                       'gui_Singleton',  gui_Singleton, ...
                       'gui_OpeningFcn', @easy_fft_1_OpeningFcn, ...
                       'gui_OutputFcn',  @easy_fft_1_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 easy_fft_1 is made visible.
    function easy_fft_1_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 easy_fft_1 (see VARARGIN)
    
    % Choose default command line output for easy_fft_1
    handles.output = hObject;
    
    % Update handles structure
    guidata(hObject, handles);
    
    %init
    global ph;
    global L;
    global Fs;
    global NFFT;
    global x1;
    global x2;
    
    ph=0;
    L=1024;
    Fs=20000;
    NFFT=1024;
    x1=0;
    x2=0;
    
    global t_mv;
    global t_mi;
    t_mv =0;
    t_mi =0;
    
    
    % UIWAIT makes easy_fft_1 wait for user response (see UIRESUME)
    % uiwait(handles.figure1);
    
    
    % --- Outputs from this function are returned to the command line.
    function varargout = easy_fft_1_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 selection change in listbox1.
    function listbox1_Callback(hObject, eventdata, handles)
    % hObject    handle to listbox1 (see GCBO)
    % eventdata  reserved - to be defined in a future version of MATLAB
    % handles    structure with handles and user data (see GUIDATA)
    
    % Hints: contents = cellstr(get(hObject,'String')) returns listbox1 contents as cell array
    %        contents{get(hObject,'Value')} returns selected item from listbox1
    
    
    % --- Executes during object creation, after setting all properties.
    function listbox1_CreateFcn(hObject, eventdata, handles)
    % hObject    handle to listbox1 (see GCBO)
    % eventdata  reserved - to be defined in a future version of MATLAB
    % handles    empty - handles not created until after all CreateFcns called
    
    % Hint: listbox 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
    
    
    
    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
    global Fs;
    Fs=str2double(get(hObject,'String'));
    fft_paras_dis(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
    
    % 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 selection change in popupmenu1.
    function popupmenu1_Callback(hObject, eventdata, handles)
    % hObject    handle to popupmenu1 (see GCBO)
    % eventdata  reserved - to be defined in a future version of MATLAB
    % handles    structure with handles and user data (see GUIDATA)
    
    % Hints: contents = cellstr(get(hObject,'String')) returns popupmenu1 contents as cell array
    %        contents{get(hObject,'Value')} returns selected item from popupmenu1
    global NFFT;%1024 512 256 128 64 32 16
    contents = cellstr(get(hObject,'String'));
    t=contents{get(hObject,'Value')};
    NFFT=str2double(t);
    fft_paras_dis(handles);
    
    
    
    % --- Executes during object creation, after setting all properties.
    function popupmenu1_CreateFcn(hObject, eventdata, handles)
    % hObject    handle to popupmenu1 (see GCBO)
    % eventdata  reserved - to be defined in a future version of MATLAB
    % handles    empty - handles not created until after all CreateFcns called
    
    % Hint: popupmenu 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
    
    
    % --------------------------------------------------------------------
    function menu1_t_Callback(hObject, eventdata, handles)
    % hObject    handle to menu1_t (see GCBO)
    % eventdata  reserved - to be defined in a future version of MATLAB
    % handles    structure with handles and user data (see GUIDATA)
    
    
    % --- 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)
    fft_plot(handles);
    
    % --------------------------------------------------------------------
    function openfile_1_ClickedCallback(hObject, eventdata, handles)
    % hObject    handle to openfile_1 (see GCBO)
    % eventdata  reserved - to be defined in a future version of MATLAB
    % handles    structure with handles and user data (see GUIDATA)
    %----
    [filename, pathname] = uigetfile( ...
        {'*.txt', 'All TXT-Files (*.txt)'; ...
            '*.*','All Files (*.*)'}, ...
        'Select Address Book');
    global File;
    % If "Cancel" is selected then return
    if isequal([filename,pathname],[0,0])
        return
        % Otherwise construct the full file name and _and load the file.
    else
        File = fullfile(pathname,filename);
    end
    
    %open and read data from file
    %
    %file_name = ['fir_1_input.txt'];
    infile = fopen (File, 'r');
    % read in data from the file
    data = fscanf(infile, '%f');
    fclose(infile);
    global dd;
    global L;
    dd = data;%此处是列向量,需要转化为行向量
    dd = dd';
    L=length(dd);% Length of signal
    fft_paras_dis(handles);
    
    
    % --- Executes on slider movement.
    function slider1_Callback(hObject, eventdata, handles)
    % hObject    handle to slider1 (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,'Value') returns position of slider
    %        get(hObject,'Min') and get(hObject,'Max') to determine range of slider
    global ph;
    global L;
    global Fs;
    global NFFT;
    ph=(get(hObject,'Value')/ get(hObject,'Max'))*1;
    fft_paras_dis(handles);
    fft_plot(handles);
    
    
    
    
    
    % --- Executes during object creation, after setting all properties.
    function slider1_CreateFcn(hObject, eventdata, handles)
    % hObject    handle to slider1 (see GCBO)
    % eventdata  reserved - to be defined in a future version of MATLAB
    % handles    empty - handles not created until after all CreateFcns called
    
    % Hint: slider controls usually have a light gray background.
    if isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
        set(hObject,'BackgroundColor',[.9 .9 .9]);
    end
    
    
    
    
    
    function fft_plot(handles)
    %--------
    global Fs; % Sampling frequency      
    global NFFT; % = 512;
    global dd;
    global L;
    T = 1/Fs;  % Sample time
    %L=length(dd);% Length of signal
    t = (0:L-1)*T*1000;                % Time vector/ms
    % Sum of a 50 Hz sinusoid and a 120 Hz sinusoid
    %x = 0.7*sin(2*pi*50*t) + sin(2*pi*120*t); 
    %y = x + 2*randn(size(t));     % Sinusoids plus noise
    %figure;
    %axes(handles.axes1);
    %subplot(2,1,1);
    cla(handles.axes1);
    plot(handles.axes1,t(1:L),dd(1:L),'b');%plot(Fs*t(1:50),y(1:50));
    title(handles.axes1,'Signal Corrupted with  Noise')
    xlabel(handles.axes1,'time (milliseconds)')
    %-------------------------------------------------
    %NFFT = 2^nextpow2(L); % Next power of 2 from length of y
    global ph;
    global x1;
    global x2;
    x1=ceil(ph*L+0.0001);%ceil(0)=0,so we add extral 0.001
    if x1+NFFT-1<=L
        x2=x1+NFFT-1;
        dd_t = dd(x1:x2);
    else
        x2=L;
        dd_t = [dd(x1:x2) zeros(1,NFFT-(x2-x1)-1)];%数据不够则补零
    end
    %
     
    %
    YY=fft(dd_t,NFFT);
    Y = YY/(x2-x1+1);
    f = Fs/2*linspace(0,1,NFFT/2+1);
    A =2*abs(Y(1:NFFT/2+1));
    % Plot single-sided amplitude spectrum.
    %subplot(2,1,2);
    %plot(handles.axes2,f,A) ;
    A(1)=0;%delete the DC 
    cla(handles.axes2);
    stem(handles.axes2,f,A,'b');
    title(handles.axes2,'Single-Sided Amplitude Spectrum of y(t)')
    xlabel(handles.axes2,'Frequency (Hz)')
    ylabel(handles.axes2,'|Y(f)|')
    %%
    disp('-----------------------------------------------');
    %YY(10:end)=0;
    
    global t_mv;
    global t_mi;
    [t_mv ,t_mi]=max(A(1:NFFT/2+1));
    
    pp=t_mi;
    YY(pp)=0;YY(NFFT+2-pp)=0;
    pp=t_mi-1;
    if pp<1
      pp=1;
    end
    YY(pp)=0;YY(NFFT+2-pp)=0;
    pp=t_mi+1;
    YY(pp)=0;YY(NFFT+2-pp)=0;
    %%
    disp('YY:');
    disp(YY);
    idd=ifft(YY,NFFT);
    disp('idd:');
    disp(idd);
    disp('length - idd:');
    disp(length(idd));
    hold on;
    x2_t = x1+length(idd)-1;
    if(x2_t)>length(t)
        x2_t = length(t);
    end  
    plot(handles.axes1,t(x1:x2_t),abs(idd(1:x2_t-x1+1)),'r');
    %
    AYY=2*abs(YY(1:NFFT/2+1)/(x2-x1+1));%2*abs(Y(1:NFFT/2+1));
    AYY(1)=0;%DC
    stem(handles.axes2,f,AYY,'r') ;
    disp('-----------------------------------------------');
    
    function fft_paras_dis(handles)
    %
    global ph;
    global L;
    global Fs;
    global NFFT;
    global x1;
    global x2;
    set(handles.text_paras,'String',['ph:' num2str(ph) '  L:' num2str(L)  '  N:' num2str(NFFT)  '  Fs:' num2str(Fs) '  x1:' num2str(x1)  '  x2:'  num2str(x2)]);
    global t_mv;
    global t_mi;
    global File;
    set(handles.text_maxs,'String',['mv:' num2str(t_mv) '  mi:' num2str(t_mi) '   ' File]);
    
    
    
    %%
    
    
    % --------------------------------------------------------------------
    function uipushtool5_ClickedCallback(hObject, eventdata, handles)
    % hObject    handle to uipushtool5 (see GCBO)
    % eventdata  reserved - to be defined in a future version of MATLAB
    % handles    structure with handles and user data (see GUIDATA)
    gen_txt_fft;
    disp('gen txt ok!');
    
  • 相关阅读:
    android prgoressBar setProgressDrawable 在4.0系统式正常,在2.3系统上不能正常使用的问题
    android 动态控制状态栏显示和隐藏
    静态成员变量和静态成员函数(static)
    WPF中如何使用BusyIndicator
    C# 面试题 二
    C#面试题及答案 一 <转来的,貌似有看评论说有错误,正在一个个纠正中…… 也望园友们指出>
    [转载]如何用Visual Studio制作安装包
    WPF九宫格图片自定义皮肤(新博速读2.0)
    附加题
    附加题 回答问题
  • 原文地址:https://www.cnblogs.com/time93/p/13246437.html
Copyright © 2011-2022 走看看