Question

Problem

I am running a Matlab script that creates a mesh geometry stored within a ‘.m’ file; all of this is for running with a finite element solver. I give it a number of elements, and it creates a mesh of quadrilaterals that fit within a boundary; two examples of the geometry are shown in Figure 1 and Figure 2. When I use it for only a single number of elements, it works great. On the other hand, when I try to use the mesh-generating script within a loop, varying the number of desired elements (n = 1,2,4,...32,64), the file reads as though I just ran it at n = 1. It’s as if the ‘.m’ file was not rewritten.

Mesh with 10 elements Figure 1. Mesh with 10 quadrilateral elements.

Mesh with 40 elements Figure 2. Mesh with 40 quadrilateral elements.

Current Approach

for i = 1:4
    n = 2^i % n stores the desired number of elements (2^i is only an example)
    % Check if '.m' file exists in current directory
    allFiles = dir(currentFolder)
    allNames = {allFiles.name}
    check = ismember('p4.m',allNames);
    % If so, delete the file
    if check
        delete 'p4.m'
    end
    % Create the 'p4.m' file
    p4Mesh(n)
end

p4Mesh Function Summary:

function p4Mesh
close all; 
inputfile = 'p4.m'; 
% open file 
fid = fopen(inputfile,'w'); 
fprintf(fid,'MESH CODE HERE') 
% Close file
[st] = fclose('all'); 
end
Was it helpful?

Solution

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.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top