2016-07-15 214 views
0

我有一个m文件,它会截断信号并根据截止频率(Fc)应用滤波器。将MATLAB GUI连接到.m文件

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

我创建具有情节和滑块(用于切断frequcy),其在代码显示为f.fc enter image description here

的GUI当我创建GUI时,Matlab自动为我创建了一个代码(我必须说,我不懂那么多)

GUI代码:

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)

我想要做的是,我希望将GUI连接到MY M脚本,当用户sildes滑块。它应该自动显示图形的变化,并且当他点击应用时。滑块的值应该被采用并且应该在我的m文件中可用。

任何线索都会有帮助。

+0

为什么标记为'C++'? –

+0

对不起,我改变了 – user5603723

回答

0

从代码示例粘贴,我假设你正在使用MATLAB GUIDE。
我们假设滑块控件的名称是“slider1”。
使用GUIDE添加“滑块”的回调函数。
它会在您的代码中创建一个函数“function slider1_Callback(hObject, eventdata, handles)”。
现在,要从滑块移动中获取所选值,请使用“hObject”的“get”函数。
例如SliderVal=get(hObject,'Value');

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

根据滑块值,收到您需要重新绘制绘图区域。 我希望这可以帮助你预期的线索。

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

  1. 将GUI图形的“HandleVisibility”属性(通过GUIDE)设置为“ON”。
  2. 从M-script中调用“figures = get(0,'Children');”以获取所有未清数字的列表。这将给图形手柄的向量。
  3. 扫描儿童列表并获取您的应用程序的句柄。 (这可以通过使用属性get(figures(1),'Name')完成)。
  4. 假设您发现该句柄,再次重复相同的过程以从中获取孩子。 get(figHandle,'Children')
  5. 扫描儿童并找到类似于步骤3中的方法的滑块控制手柄。
  6. 现在您可以访问控件及其数据。


我希望你能理解它。

+0

嘿,谢谢你的回答。我如何得到这个值在我的另一个m文件中,我使用这个值来应用过滤器 – user5603723