Thread Subject: Trigger m-file based on file creation

Subject: Trigger m-file based on file creation

From: Ethan

Date: 23 Sep, 2007 04:30:21

Message: 1 of 12

Sorry if this is trivial, but I feel like I'm missing
something. I'm writing a GUI that I would like to do the
following:

> Any time a *.txt file is written to a directory
    > Load the datafile into matlab memory
    > Plot the data associated with the file
    > Move the datafile to an /archive directory

I can do all these things manually - with uigetfile and
various load and plot functions and movefile. But I can't
figure out how to 'trigger' matlab to do it
automatically.

A link to a similar type of problem or just a hint on the
commands to use would be appreciated.

Thanks.

Subject: Trigger m-file based on file creation

From: "G.A.M.

Date: 23 Sep, 2007 05:26:41

Message: 2 of 12

On Sep 23, 12:30 am, "Ethan " <proper.nospam...@mit.edu> wrote:
> Sorry if this is trivial, but I feel like I'm missing
> something. I'm writing a GUI that I would like to do the
> following:
>
> > Any time a *.txt file is written to a directory
>
> > Load the datafile into matlab memory
> > Plot the data associated with the file
> > Move the datafile to an /archive directory
>
> I can do all these things manually - with uigetfile and
> various load and plot functions and movefile. But I can't
> figure out how to 'trigger' matlab to do it
> automatically.
>
> A link to a similar type of problem or just a hint on the
> commands to use would be appreciated.
>
> Thanks.

I can't tell you how to do it, but you asked for some hints, so I'll
see if I can offer a few ideas that might help. If Matlab doesn't have
this functionality somewhere, you need to implement the Observer
pattern. You'll probably want to write some code that watches the
files in a folder. That code will run on a separate thread from your
UI code. It will use a timer and the thread will sleep until the timer
wakes it up. It will then check the folder. If it finds what you are
asking it to look for, it will trigger an event. Your other code (on
your main thread) will be subscribed to this event (in keeping with
the Observer pattern).

I'm very new to Matlab. The only thing I know of in Matlab that's
similar to this is the BytesAvailableFcn that does exactly what you
want except in response to bytes becoming available on a serial port.
Maybe you can look at this and get some ideas or find something
similar that is suited to your purpose.

Subject: Trigger m-file based on file creation

From: Jos

Date: 23 Sep, 2007 13:09:47

Message: 3 of 12

"Ethan " <proper.nospammer@mit.edu> wrote in message
<fd4q4t$o9d$1@fred.mathworks.com>...
> Sorry if this is trivial, but I feel like I'm missing
> something. I'm writing a GUI that I would like to do the
> following:
>
> > Any time a *.txt file is written to a directory
> > Load the datafile into matlab memory
> > Plot the data associated with the file
> > Move the datafile to an /archive directory
>
> I can do all these things manually - with uigetfile and
> various load and plot functions and movefile. But I
can't
> figure out how to 'trigger' matlab to do it
> automatically.
>
> A link to a similar type of problem or just a hint on the
> commands to use would be appreciated.
>
> Thanks.
>
>
>
>

help timer
will get you started

Jos

Subject: Trigger m-file based on file creation

From: Ethan

Date: 23 Sep, 2007 14:40:36

Message: 4 of 12

Yeap... Timer was what I needed. Thanks a ton.

Subject: Trigger m-file based on file creation

From: Rik

Date: 24 Sep, 2007 13:51:22

Message: 5 of 12

"Ethan " <proper.nospammer@mit.edu> wrote in message
<fd5tt4$ebt$1@fred.mathworks.com>...
> Yeap... Timer was what I needed. Thanks a ton.

I was looking for the same thing, only I don't understand
how timer could help here: Whenever a file is created, I
would like to trigger a piece of Matlab code. As far as I
see now, with timer I can only check for new files at
certain times.

Is it also possible to somehow get it triggered by the
creation of a file?

Cheers,
Rik

Subject: Trigger m-file based on file creation

From: roberson@ibd.nrc-cnrc.gc.ca (Walter Roberson)

Date: 24 Sep, 2007 14:13:51

Message: 6 of 12

In article <fd8fcq$pn4$1@fred.mathworks.com>,
Rik <nospammerno@gmail.com> wrote:

>I was looking for the same thing, only I don't understand
>how timer could help here: Whenever a file is created, I
>would like to trigger a piece of Matlab code. As far as I
>see now, with timer I can only check for new files at
>certain times.

>Is it also possible to somehow get it triggered by the
>creation of a file?

You haven't indicated here which OS you are using. Ethan's
reference to .txt vaguely implies that he is using MS Windows,
but I don't know the OS for you.

I suggest that you see this thread from a couple of weeks ago:

http://groups.google.ca/group/comp.soft-sys.matlab/browse_thread/thread/9a52375a289e8705/61a58826b3ad395b
--
  All is vanity. -- Ecclesiastes

Subject: Trigger m-file based on file creation

From: Ethan

Date: 24 Sep, 2007 14:16:12

Message: 7 of 12

Right. Timer doesn't actually solve the specific 'trigger
based on file creation' problem but it did what I needed
it for my code.

I'm not sure what the resource impact would be but I would
think you could dial down the time on timer to the point
that the system event would essentially be triggered
within milliseconds of the event - but you're right that
it isn't the same thing as triggering based on file
creation.

Subject: Trigger m-file based on file creation

From: Rik

Date: 24 Sep, 2007 15:56:27

Message: 8 of 12

roberson@ibd.nrc-cnrc.gc.ca (Walter Roberson) wrote in
message <fd8gmv$442$1@canopus.cc.umanitoba.ca>...
> In article <fd8fcq$pn4$1@fred.mathworks.com>,
> Rik <nospammerno@gmail.com> wrote:
>
> >I was looking for the same thing, only I don't understand
> >how timer could help here: Whenever a file is created, I
> >would like to trigger a piece of Matlab code. As far as I
> >see now, with timer I can only check for new files at
> >certain times.
>
> >Is it also possible to somehow get it triggered by the
> >creation of a file?
>
> You haven't indicated here which OS you are using. Ethan's
> reference to .txt vaguely implies that he is using MS Windows,
> but I don't know the OS for you.
>
> I suggest that you see this thread from a couple of weeks ago:
>
>
http://groups.google.ca/group/comp.soft-sys.matlab/browse_thread/thread/9a52375a289e8705/61a58826b3ad395b
> --
> All is vanity. --
Ecclesiastes

I'm afraid it's on Windows ;) (nofi) (Windows 2003 server).

@Ethan: I suppose I could do that, it's just a little more
dirty then using services that are designed to do this
stuff. For instance, using a timer I must also check whether
the file is actually finished, or that another application
is still writing to it.

Subject: Trigger m-file based on file creation

From: roberson@ibd.nrc-cnrc.gc.ca (Walter Roberson)

Date: 24 Sep, 2007 16:26:25

Message: 9 of 12

In article <fd8mnb$pm7$1@fred.mathworks.com>,
Rik <nospammerno@gmail.com> wrote:
>> In article <fd8fcq$pn4$1@fred.mathworks.com>,
>> Rik <nospammerno@gmail.com> wrote:

>> >Whenever a file is created, I
>> >would like to trigger a piece of Matlab code.

>I'm afraid it's on Windows ;) (nofi) (Windows 2003 server).

>@Ethan: I suppose I could do that, it's just a little more
>dirty then using services that are designed to do this
>stuff. For instance, using a timer I must also check whether
>the file is actually finished, or that another application
>is still writing to it.

However, if you -did- get notification of file creation,
perhaps by some kind of system monitoring service, then you
would still have the difficulty that the file might still be in
use. What you want to know, it sounds like, is not that the file
is created, but rather that the file is "ready for use" -- created
and finished being written to and everything flushed out. That is
generally a much tougher task even on OS's that have integrated
file notification services.

A common work-around for the "the file might not be finished"
problem, is to have the files created under a different name
(or in a different directory on the same filesystem) and then
when the application finishes with the file, it renames it to
the proper format or to the production directory. Then when you
see the file with the right name format appear, you know that it
is already ready for use.
--
  All is vanity. -- Ecclesiastes

Subject: Trigger m-file based on file creation

From: Rik

Date: 24 Sep, 2007 16:38:15

Message: 10 of 12

roberson@ibd.nrc-cnrc.gc.ca (Walter Roberson) wrote in
message <fd8ofh$e5v$1@canopus.cc.umanitoba.ca>...
> In article <fd8mnb$pm7$1@fred.mathworks.com>,
> Rik <nospammerno@gmail.com> wrote:
> >> In article <fd8fcq$pn4$1@fred.mathworks.com>,
> >> Rik <nospammerno@gmail.com> wrote:
>
> >> >Whenever a file is created, I
> >> >would like to trigger a piece of Matlab code.
>
> >I'm afraid it's on Windows ;) (nofi) (Windows 2003 server).
>
> >@Ethan: I suppose I could do that, it's just a little more
> >dirty then using services that are designed to do this
> >stuff. For instance, using a timer I must also check whether
> >the file is actually finished, or that another application
> >is still writing to it.
>
> However, if you -did- get notification of file creation,
> perhaps by some kind of system monitoring service, then you
> would still have the difficulty that the file might still
be in
> use. What you want to know, it sounds like, is not that
the file
> is created, but rather that the file is "ready for use" --
created
> and finished being written to and everything flushed out.
That is
> generally a much tougher task even on OS's that have
integrated
> file notification services.
>
> A common work-around for the "the file might not be finished"
> problem, is to have the files created under a different name
> (or in a different directory on the same filesystem) and then
> when the application finishes with the file, it renames it to
> the proper format or to the production directory. Then
when you
> see the file with the right name format appear, you know
that it
> is already ready for use.
> --
> All is vanity. --
Ecclesiastes

So even with a notification, it's likely that I still have
to check whether it's also ready.
Maybe I do have to resort to the solution with the timer then.
 I have no control over how the file is written, but I guess
I can just check by attempting to open it for write acces.

Thanks,
Rik

Subject: Trigger m-file based on file creation

From: roberson@ibd.nrc-cnrc.gc.ca (Walter Roberson)

Date: 24 Sep, 2007 21:25:33

Message: 11 of 12

In article <fd8p5n$6r4$1@fred.mathworks.com>,
Rik <nospammerno@gmail.com> wrote:
[re: files appearing]

>So even with a notification, it's likely that I still have
>to check whether it's also ready.

Right.

>Maybe I do have to resort to the solution with the timer then.
> I have no control over how the file is written, but I guess
>I can just check by attempting to open it for write acces.

That wouldn't work in a POSIX system; multiple processes are
allowed to open the same file for write access. The closest
that POSIX.1-1990 comes is "advisory" file locks, which cannot
even be attempted until you have already opened the file and
you then fcntl() the advisory lock into place. As you indicate
you have no control over how the file is written, you can be
pretty sure that the writer will not be using advisory locks.

The POSIX locking mechanisms you can use in later POSIX such
as POSIX.1-1996 also suffer from much the same limitations,
though you might be able to get further if the reader and
writer were both considered threads of the same process.

But I think you mentioned Windows; evidence suggests that Windows
does keep track of whether a file is open by another process
or not; I do not have any information as to how to tell in Windows
(and I don't know if it is cooperative or not.)
--
  If you lie to the compiler, it will get its revenge. -- Henry Spencer

Subject: Trigger m-file based on file creation

From: Rik

Date: 25 Sep, 2007 07:37:57

Message: 12 of 12

roberson@ibd.nrc-cnrc.gc.ca (Walter Roberson) wrote in
message <fd9a0d$7l9$1@canopus.cc.umanitoba.ca>...
> In article <fd8p5n$6r4$1@fred.mathworks.com>,
> Rik <nospammerno@gmail.com> wrote:
> [re: files appearing]
>
> >So even with a notification, it's likely that I still have
> >to check whether it's also ready.
>
> Right.
>
> >Maybe I do have to resort to the solution with the timer
then.
> > I have no control over how the file is written, but I guess
> >I can just check by attempting to open it for write acces.
>
> That wouldn't work in a POSIX system; multiple processes are
> allowed to open the same file for write access. The closest
> that POSIX.1-1990 comes is "advisory" file locks, which cannot
> even be attempted until you have already opened the file and
> you then fcntl() the advisory lock into place. As you indicate
> you have no control over how the file is written, you can be
> pretty sure that the writer will not be using advisory locks.
>
> The POSIX locking mechanisms you can use in later POSIX such
> as POSIX.1-1996 also suffer from much the same limitations,
> though you might be able to get further if the reader and
> writer were both considered threads of the same process.
>
> But I think you mentioned Windows; evidence suggests that
Windows
> does keep track of whether a file is open by another process
> or not; I do not have any information as to how to tell in
Windows
> (and I don't know if it is cooperative or not.)
> --
> If you lie to the compiler, it will get its revenge. --
Henry Spencer

Well, then I'll just have to try to move it, I'm pretty sure
Windows will prevent that when something else is writing to
it (no worry, I wil actually test this before i'll put it in
production ;)

Tags for this Thread

Everyone's Tags:

Add a New Tag:

Separated by commas
Ex.: root locus, bode

What are tags?

A tag is like a keyword or category label associated with each thread. Tags make it easier for you to find threads of interest.

Anyone can tag a thread. Tags are public and visible to everyone.

Tag Activity for This Thread
Tag Applied By Date/Time
realtime Ethan 23 Sep, 2007 00:35:04
file management Ethan 23 Sep, 2007 00:35:04
rssFeed for this Thread

Contact us at files@mathworks.com