Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8073301
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T14:17:49+00:00 2026-06-05T14:17:49+00:00

I would like to access Matlab from Python (on Windows, remotely and via COM-interface).

  • 0

I would like to access Matlab from Python (on Windows, remotely and via COM-interface). My goal is: Matlab is doing some work and permanently changes the value of a certain variable. I need to know when the value exceeds some constant. Right now, I’m polling Matlab for the value of that variable in an indefinite loop which breaks on exceeding the value. However, I would like to let Matlab do the work and tell me when that is the case, while I’m lazily sitting there listening. Is there a way to achieve this, and how is it done best? I have thought of defining a callback function I’m passing to Matlab, which on the exceed-event triggers a break out of a non-busy-wait-loop in Python, but I doubt it would work. I’m not very experienced neither in Matlab nor Python, so cues are much appreciated.

There’s a lot of other code involved, but basically right now it is like

connectToMatlab(*args)
while True:
    val = getValueFromMatlab()
    if val > constant or timeout: break

What I have in mind is

def breakLoop():
    ...  

connectToMatlab(breakLoop, *args)
while True:
    time.sleep(1) # or some alternate non-busy-wait

and then let Matlab invoke breakLoop() upon val > constant. However, I don’t know if it is possible to let Matlab do that with a callback and if yes, how to implement such a breakLoop()-Function.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-05T14:17:51+00:00Added an answer on June 5, 2026 at 2:17 pm

    You can go about this the other way, and use the file system as a way to pass the messages between MATLAB and Python.

    Inside your MATLAB code, each time you change the variable, check if it exceed some threshold. If it does, create a new file in a predetermined location. Think of this as triggering an event.

    Now inside your python code, use some of the available ways to the listen to changes in the file system, and respond by indicating some variable to break the loop.


    EDIT

    Here is a skeleton of the solution proposed:

    matlab_script.m

    %# directory that Python code is watching for modifications
    dirPath = 'some_directory';
    
    x = 0;
    for i=1:1000
        %# some lengthy operation
        pause(0.5)
        x = x + 1;
    
        %# check if variable exceeds threshold
        if x > 10
            %# save the workspace to MAT-file inside the directory watched.
            %# this shall trigger the notification in Python
            save( fullfile(dirPath,'out.mat') )
            break
        end
    end
    

    python_code.py

    import os, sys, time
    import win32file, win32event, win32con
    
    # stub your functions in my case
    def connectToMatlab():
      pass
    def getValueFromMatlab():
      return 99
    
    # path to predetermined directory to watch
    dirPath = "some_directory"
    dirPath = os.path.abspath(dirPath)
    
    # start/connect to a MATLAB session, running the script above
    connectToMatlab()
    
    # set up folder watching (notify on file addition/deletion/renaming)
    print "Started watching '%s' at %s" % (dirPath, time.asctime())
    change_handle = win32file.FindFirstChangeNotification(
      dirPath, 0, win32con.FILE_NOTIFY_CHANGE_FILE_NAME)
    
    # time-out in 10 sec (win32event.INFINITE to wait indefinitely)
    timeout = 10000
    
    try:
      # block/wait for notification
      result = win32event.WaitForSingleObject(change_handle, timeout)
    
      # returned because of a change notification
      if result == win32con.WAIT_OBJECT_0:
        # retrieve final result from MATLAB
        print "MALTAB variable has exceeded threshold at %s" % time.asctime()
        val = getValueFromMatlab()
    
      # timed out
      elif result == win32con.WAIT_TIMEOUT:
        print "timed-out after %s msec at %s" % (timeout,time.asctime())
        val = None    # maybe to indicate failure
    
    finally:
      # cleanup properly
      win32file.FindCloseChangeNotification(change_handle)
    
    # work with val
    print val
    

    The WaitForSingleObject function start by checking the state of the specified object. If it is nonsignaled, the calling thread enters an efficient wait state and consumes very little processor time while waiting until the object is signaled (or the time-out interval elapses).

    You see when the thread references the object which is in non-signaled state, there is an immediate context switch i.e. it is taken down from the processor and put into a wait/sleep mode. Later when the object is signaled, the thread is put back to the runnable queue and is ready for execution.

    In this kind of waiting there is no wastage of CPU cycles in wait state, although there is a some overhead in context switching.

    Compare this against the “poll-and-wait” approach, where the thread waits and checks for the state of the object of interest in some kind of a loop. This is known as spin or busy wait, which can prove to be a wastage of CPU cycles.

    Now thanks to pywin32 module, we can use use those WaitFor... functions directly. The implementation should be a straightforward port of the standard example given in MSDN.

    Alternatively you can use the PyQt library with its QFileSystemWatcher class instead of directly using the Win32 API.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I would like to access Windows share (ex. \backupserver\backups) from Python script. Share is
I would like to access a file system file from within SharePoint, programmatically via
I would like to access a web page from a python program. I have
There I am writing a Python application on win7. I would like to display/access
In matlab I would like to read from a structured, and rather big, file
i would like to access li (id-67) via li (id-68) i tried $(#68).parent('li').attr(id) and
I would like to access the array with something like array[5000][440] meaning 5000ms from
I would like to access the ActionController::Request from within the environments/development.rb file so that
I would like to access a bazaar repository and pull code from it with
I start with Matlab and would like to know how could I access to

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.