Oops - I mistakenly wrote TEMPNAME above in place of TEMPDIR. Also, the issue seems to be my fault because I changed TEMPDIR while debugging this code. However, I think it is cleaner to use TEMPNAME to create the file names. Either way, the file names displayed by VISDIFF will generally not correspond to the user's variable names.
I ran into a few problems running this code:
1. It uses the directory given by the environment variable TEMPNAME. Oddly, though, TEMPNAME is incorrect on my machine.
2. It calls VISDIFF, which works asynchronously. As a result, STRUCTCOMPVIS deletes the temporary files before VISDIFF has managed to open them.
3. It displays the contents of the structures in the command window as well as the comparison window.
4. The issue regarding lengths of field names is due to an unnecessary parameter handed to VISDIFF.
Here is some code that fixes all these problems:
% Sort structures, to ignore order
s1 = orderfields(s1);
s2 = orderfields(s2);
% Write structures to temp files
structInStringFormat = evalc('disp(s1)');
f1 = tempname;
fid = fopen(f1,'w+');