将MATLAB GUI连接到.m文件

时间:2016-07-15 07:54:43

标签: matlab user-interface

我有一个m文件,它会切断信号并根据截止频率(Fc)应用滤波器。

M档案:



classdef Container < handle
    properties
     segments = struct('signal', {}, 'time', {},'ref',{}); %empty structure with correct fields
   end
    
    methods
          function this = addsignal(this, signal, time,fc)
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%chopping of the signals%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
            interval = diff(time);
            [~, locations] = findpeaks(interval,'THRESHOLD',0.7);
            edges = [0; locations; numel(signal)+1];
            newsegments = struct('signal', cell(numel(edges)-1, 1), 'time', cell(numel(edges)-1, 1)); 
             %this loop works for no peaks, 1 peak and more than one peak (because of the 0 and numel+1)
             for edgeidx = 1 : numel(edges) - 1
                newsegments(edgeidx).signal = signal(edges(edgeidx)+1 : edges(edgeidx+1)-1);
                newsegments(edgeidx).time =   time(edges(edgeidx)+1 : edges(edgeidx+1)-1);
             end


%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%filtering%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
             f = ltiFilter.PT1(); % another class which has filters
             f.Ts = mean(diff(time));
             f.fc = fc; % i want to set this value from the slider%%%%%
             f.zeroPhaseShift = 1;
             for i = 1:length(newsegments)
                 newsegments(i).ref = f.eval(newsegments(i).signal,newsegments(i).signal(1)); % application of the filter.
                 newsegments(i).ref = newsegments(i).ref';
             end
             
            this.segments = [this.segments; newsegments];
          end
     end  
end
&#13;
&#13;
&#13;

我创建了一个GUI,它有一个绘图和一个滑块(用于截断频率),在代码中显示为f.fc enter image description here

当我创建GUI时,Matlab会自动为我创建一个代码(我必须说,我很难理解)

GUI代码:

&#13;
&#13;
function varargout = GUI(varargin)
% GUI MATLAB code for GUI.fig
%      GUI, by itself, creates a new GUI or raises the existing
%      singleton*.
%
%      H = GUI returns the handle to a new GUI or the handle to
%      the existing singleton*.
%
%      GUI('CALLBACK',hObject,eventData,handles,...) calls the local
%      function named CALLBACK in GUI.M with the given input arguments.
%
%      GUI('Property','Value',...) creates a new GUI or raises the
%      existing singleton*.  Starting from the left, property value pairs are
%      applied to the GUI before GUI_OpeningFcn gets called.  An
%      unrecognized property name or invalid value makes property application
%      stop.  All inputs are passed to GUI_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 GUI

% Last Modified by GUIDE v2.5 15-Jul-2016 09:37:09

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

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

% Update handles structure
guidata(hObject, handles);

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


% --- Outputs from this function are returned to the command line.
function varargout = GUI_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 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


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


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


% --- 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)
&#13;
&#13;
&#13;

我想要做的是,当用户使用滑块时,我想将GUI连接到我的m脚本。它应该自动显示图表中的更改,并在他点击应用时显示。应该采用滑块的值,并且应该在我的m文件中可用。

任何线索都会有所帮助。

1 个答案:

答案 0 :(得分:0)

从粘贴的代码示例中,我假设您正在使用MATLAB GUIDE。
假设滑块控件的名称是&#34; slider1&#34;。
为&#34;滑块&#34;添加回调函数使用GUIDE。
它将创建一个功能&#34; function slider1_Callback(hObject, eventdata, handles)&#34;在你的代码中。
现在,要从滑块移动中选择值,请使用&#34; get&#34;功能与&#34; hObject&#34;。
例如。 SliderVal=get(hObject,'Value');

现在,如果您想知道其他回调中滑块选择的值(例如“应用”按钮) 使用句柄结构。
例如:SliderVal=get(handles.slider1,'Value');

根据滑块值,您需要重新绘制绘图区域。 我希望这有助于你所期待的线索。

<强> EDIT1:
对于后续评论,如何从其他M文件中获取数据:
这将非常棘手。因为,你需要知道其他M文件中滑块控件的句柄。
其中一种方法是首先处理数字。

  1. 设置&#34; HandleVisibility&#34; GUI图形的属性(通过GUIDE)到&#34; ON&#34;。
  2. 致电&#34; figures = get(0,'Children');&#34;从M-script获取所有未结数字的列表。这将给出图形句柄的矢量。
  3. 扫描子项列表并处理您的应用程序。 (这可以通过使用属性get(figures(1),'Name'))来完成。
  4. 让我们假设您找到了句柄,再次重复相同的过程以让孩子从中获取。 get(figHandle,'Children')
  5. 扫描孩子并找到滑块控制手柄,类似于步骤3中的方法。
  6. 现在您可以访问控件及其数据。

  7. 我希望你理解它。