Summary
The p4
function definition might be getting cached so you're not picking up the new versions you write out to disk on subsequent calls to p4()
. Use clear p4
to drop the cached definition and force it to re-read p4.m
each time you update it.
Details
You may be running in to an issue with Matlab's function definition caching. When you call a function or class for the first time, Matlab reads the definition in from the .m
file on disk, compiles it, and runs it, and then keeps that compiled definition cached. It's pretty unlikely that the file writing is lagging the Matlab loop, since fopen/fprintf/fclose are synchronous, and Matlab is single-threaded. But Matlab's function-loading mechanism might not be noticing the changes on disk as fast as you need.
Matlab will re-read the function definition if it notices the definition has been changed on disk. If you save a function file from the Matlab Editor, it notices the change right away since that goes through Matlab. But if you write the file's contents from code, it looks to Matlab like an external change. Matlab gets updates about these changes in a system-dependent manner, like getting file change notification events from the operating system, or periodically polling the filesystem to check file timestamps. See doc changeNotification
and doc changeNotificationAdvanced
for details on this.
Since you're re-writing the file in a tight loop, the updates might be happening too fast for Matlab's change notification to notice. In fact, Matlab may delay the file update checks until it's done running user functions (for performance and consistency reasons), so might not do the update checks at all until it's done with your loop and returned from that script to the command line. In this case, if you called p4()
multiple times inside the loop, it would use the cached function definition from the first pass each time, and you'd see the same output from the function. This sounds like what you're describing.
Diagnosis
To see if this is what is actually happening, examine the contents of the p4.m
file on disk outside the mechanism of calling it as a Matlab function. You can use the md5sum
program available on many systems to output a one line checksum of it. Or you can call out to cat
or just re-read it using fopen/fread/fclose and print it or an md5 checksum of it to the console from within Matlab. If the file looks different on disk each pass, but you're getting the same output from calling p4()
, it's probably function caching.
Fixing
You can force Matlab to re-read a function from the current .m
file on disk by using the clear
command to drop its cached compiled definition from memory. In this case, do a clear p4;
in each pass of the loop right after you've called p4mesh
to write out the p4.m
file, and before p4
gets called again.
Doing pause
or drawnow
inside the loop may also give Matlab's function-caching mechanism a chance to notice the updated .m
file on disk. But clear p4
should be more reliable and deterministic, since it forces a re-read each time.
In the long run, it might be better if you could get your professor to give you a p4mesh
that produced .mat
data files instead of .m
function definition files, like the commenters suggested. The .mat
data files are more suited to rapidly changing definitions, and don't have this caching issue.