This is machine translation

Translated by Microsoft
Mouseover text to see original. Click the button below to return to the English version of the page.

Note: This page has been translated by MathWorks. Click here to see
To view all translated materials including this page, select Country from the country navigator on the bottom of this page.

Use .NET Events in MATLAB

These examples use the addlistener function to handle .NET events with MATLAB® callbacks.

Monitor Changes to .TXT File

This example uses the System.IO.FileSystemWatcher class in the System assembly to monitor changes to a .TXT file in the C:\work\temp folder. Create the following event handler, eventhandlerChanged.m:

function eventhandlerChanged(source,arg)
disp('TXT file changed')
end

Create a FileSystemWatcher object fileObj and watch the Changed event for files with a .txt extension in the folder C:\work\temp.

file = System.IO.FileSystemWatcher('c:\work\temp');
file.Filter = '*.txt';
file.EnableRaisingEvents = true;
addlistener(file,'Changed',@eventhandlerChanged);

If you modify and save a .txt file in the C:\work\temp folder, MATLAB displays:

TXT file changed

The FileSystemWatcher documentation says that a simple file operation can raise multiple events.

To turn off the event handler, type:

file.EnableRaisingEvents = false;

Monitor Changes to Windows Form ComboBox

This example shows how to listen for changes to values in a ComboBox on a Windows® Form. This example uses the SelectedValueChanged event defined by the System.Windows.Forms.ComboBox class.

To create this example, you must build a Windows Forms Application using a supported version of Microsoft® Visual Studio®.

  • Search the Microsoft MSDN® website for information about Windows Forms Applications.

  • For an up-to-date list of supported compilers, see Supported and Compatible Compilers.

Create a 64-bit Windows Forms Application, myForm, in your C:\work folder. Add a ComboBox control to Form1, and then add one or more items to ComboBox1. Build the application.

To add a listener to the form property, create the following MATLAB class, EnterComboData, which uses the attachListener method.

classdef EnterComboData < handle
properties
    form
end
methods
    function x = EnterComboData
        NET.addAssembly('C:\work\myForm\myForm\bin\x64\Debug\myForm.exe');
        x.form = myForm.Form1;
        Show(x.form)
        Activate(x.form)
    end
    function r = attachListener(x)
        % create listener
        r = addlistener(
            x.form.Controls.Item(0),
            'SelectedValueChanged',
            @x.anyChange);
    end
    function anyChange(~,~,~)
        % listener action if comboBox changes
        disp('Field updated')
    end
end
end

To execute the following MATLAB commands, you must create and load the application named myForm.exe. To create a form and call its attachListener method, use the EnterComboData class.

form = EnterComboData;
form.attachListener;

To trigger an event, select an item from the drop-down menu on the ComboBox. MATLAB displays:

Field updated

See Also

Related Topics