Discover MakerZone

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn more

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today

Thread Subject:
Real Time Data Loading

Subject: Real Time Data Loading

From: Patrick Wang

Date: 26 Jan, 2009 21:26:02

Message: 1 of 3

Hi

I'm asked to implement a task in my project that my program should be able to read/load data from either a text file whenever it refreshes, yet I'm very new to matlab and can anyone please help me out here?

The original idea is that the matlab program written by my supervisor is able to load a 2*511 matrix from a text file and do some correlations, and plot them. I'm asked to modify it and able to somehow load the text file every time when it updates and plot them again. I haven't had any experience on this and can anyone point me out where should I start looking at?

If say, the text file was c/p from the clipboard, can i skip using loading from the text file and instead just update the values from the clipboard and load them without using text file? I appreciate any help.

Regards,

Patrick

Subject: Real Time Data Loading

From: Walter Roberson

Date: 27 Jan, 2009 16:33:05

Message: 2 of 3

Patrick Wang wrote:

> I'm asked to implement a task in my project that my program should be able to read/load
> data from either a text file whenever it refreshes,

> The original idea is that the matlab program written by my supervisor is able to load
> a 2*511 matrix from a text file and do some correlations, and plot them. I'm asked to modify it
> and able to somehow load the text file every time when it updates and plot them again.

What you have been asked to do is not easy in Matlab, or in Windows in general.

In Matlab, you can detect that the file has changed by using the dir() function and
examining the last modification time and see if it is different than the time you recorded
on the last check. That makes it sound easy, but it isn't, because the
"last modification time" is updated for -each- write to the file, and does not
tell you when the writing programs is "finished" writing to the file. If you happen to
check while the writing program is in the middle of making changes to the file, then
the "last modification time" will have changed but the contents of the file may be
incomplete or may be a mix of the old and new information (depending exactly how the
file is being written.)

There is a bit of a hack that you can use in Windows (but which will not work on the
other operating systems that Matlab is supported on), provided that the file is not stored
on a network drive. Once you determine that the last modification time has changed,
use fopen() to open the file asking to *append* to the file. If the other program still
has the file open because it is still making changes to the file, then the fopen()
attempt will fail because the file will be locked for writing. (This is the same effect
you see when you ask to delete a file that another program is using.) Be sure to ask
to -append- to the file, though: if you accidentally ask to -write- to the file instead
then if the access is granted, the content of the file will be erased! And if you ask
to -read- the file, I can't promise that you will be refused access if another program
has the file open (though you could experiment with that.)

This hack will not work in the other operating systems that Matlab is supported on because
on all of those other operating systems, it is considered valid to have multiple programs
writing the same file at the same time (and there are elaborate rules in those operating
systems about exactly what data gets transfered into the file under what circumstances
to deal with simultaneous accesses.) On those other operating systems, it is possible
for a sufficiently privileged process to determine whether other processes are accessing
a particular file, but the methods of doing so are complex and best left to experts
(such as the author of the 'lsof' command.)

Some people choose to instead pause program execution for a brief period of time
after they detect that the file has been modified, to give the writing program
time to "finish up". The problem with this is that it assumes that the writing
program will finish up within a particular fixed time span, which is not a reliable
assumption, particularly when it comes to MS Windows -- you never know in MS Windows
when some random program is going to suddenly take over all available CPU or memory
resources for awhile (e.g., it can happen even just with the background check for
new mail.)

--
.signature note: I am now avoiding replying to unclear or ambiguous postings.
Please review questions before posting them. Be specific. Use examples of what you mean,
of what you don't mean. Specify boundary conditions, and data classes and value
relationships -- what if we scrambled your data or used -Inf, NaN, or complex(rand,rand)?

Subject: Real Time Data Loading

From: Patrick Wang

Date: 28 Jan, 2009 07:47:00

Message: 3 of 3

Walter Roberson <roberson@hushmail.com> wrote in message <1HGfl.24899$B01.16782@newsfe13.iad>...
> Patrick Wang wrote:
>
> > I'm asked to implement a task in my project that my program should be able to read/load
> > data from either a text file whenever it refreshes,
>
> > The original idea is that the matlab program written by my supervisor is able to load
> > a 2*511 matrix from a text file and do some correlations, and plot them. I'm asked to modify it
> > and able to somehow load the text file every time when it updates and plot them again.
>
> What you have been asked to do is not easy in Matlab, or in Windows in general.
>
> In Matlab, you can detect that the file has changed by using the dir() function and
> examining the last modification time and see if it is different than the time you recorded
> on the last check. That makes it sound easy, but it isn't, because the
> "last modification time" is updated for -each- write to the file, and does not
> tell you when the writing programs is "finished" writing to the file. If you happen to
> check while the writing program is in the middle of making changes to the file, then
> the "last modification time" will have changed but the contents of the file may be
> incomplete or may be a mix of the old and new information (depending exactly how the
> file is being written.)
>
> There is a bit of a hack that you can use in Windows (but which will not work on the
> other operating systems that Matlab is supported on), provided that the file is not stored
> on a network drive. Once you determine that the last modification time has changed,
> use fopen() to open the file asking to *append* to the file. If the other program still
> has the file open because it is still making changes to the file, then the fopen()
> attempt will fail because the file will be locked for writing. (This is the same effect
> you see when you ask to delete a file that another program is using.) Be sure to ask
> to -append- to the file, though: if you accidentally ask to -write- to the file instead
> then if the access is granted, the content of the file will be erased! And if you ask
> to -read- the file, I can't promise that you will be refused access if another program
> has the file open (though you could experiment with that.)
>
> This hack will not work in the other operating systems that Matlab is supported on because
> on all of those other operating systems, it is considered valid to have multiple programs
> writing the same file at the same time (and there are elaborate rules in those operating
> systems about exactly what data gets transfered into the file under what circumstances
> to deal with simultaneous accesses.) On those other operating systems, it is possible
> for a sufficiently privileged process to determine whether other processes are accessing
> a particular file, but the methods of doing so are complex and best left to experts
> (such as the author of the 'lsof' command.)
>
> Some people choose to instead pause program execution for a brief period of time
> after they detect that the file has been modified, to give the writing program
> time to "finish up". The problem with this is that it assumes that the writing
> program will finish up within a particular fixed time span, which is not a reliable
> assumption, particularly when it comes to MS Windows -- you never know in MS Windows
> when some random program is going to suddenly take over all available CPU or memory
> resources for awhile (e.g., it can happen even just with the background check for
> new mail.)
>
> --
> .signature note: I am now avoiding replying to unclear or ambiguous postings.
> Please review questions before posting them. Be specific. Use examples of what you mean,
> of what you don't mean. Specify boundary conditions, and data classes and value
> relationships -- what if we scrambled your data or used -Inf, NaN, or complex(rand,rand)?

Thank you so much! Its a decent post!!

Tags for this Thread

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.

Contact us