【问题标题】:Connecting MATLAB GUI to .m file将 MATLAB GUI 连接到 .m 文件
【发布时间】:2016-07-19 20:43:07
【问题描述】:

我有一个 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

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

当我创建 GUI 时,Matlab 自动为我创建了一个代码(我必须说,我不太了解)

界面代码:

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 连接到我的 m 脚本。它应该自动显示图表的变化,当他点击应用时。滑块的值应该被采用并且应该在我的 m 文件中可用。

任何线索都会有所帮助。

【问题讨论】:

  • 为什么这被标记为“C++”?
  • 对不起,已更改

标签: matlab user-interface


【解决方案1】:

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

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

根据滑块值,收到您需要重新绘制绘图区域。 我希望这可以作为您所期待的线索。

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

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


希望你能理解。

【讨论】:

  • 您好,谢谢您的回答。我如何在我的另一个 m 文件中获取这个值,我使用这个值来应用过滤器
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-04-23
  • 1970-01-01
  • 1970-01-01
  • 2018-03-15
  • 2015-06-17
  • 1970-01-01
相关资源
最近更新 更多