Matlab prevents external program from accessing a file when Matlab script is running
Show older comments
Hello,
I am controlling Altair Hypermesh (FEA Program) using MATLAB . My workflow consists of initializing tcl scripts using the command prompt called by MATLAB. The command prompt will open Hypermesh and start the tcl script. One of the tcl scripts that I wrote uploads a file into Hypermesh (.fem file), which was previously edited with MATLAB. However, there is an error in Hypermesh which says that the file does not exist (but it does). This error only occurs if MATLAB is running a script. The .fem file is able to be manually uploaded with no errors only if MATALAB has stopped running.
I believe that the problem has something to do with MATLAB preventing Hypermesh from accessing the .fem file. The function fclose('all') was used to close all files but this does not solve the problem.
Is the source of the problem MATLAB or Hypermesh? My computer is Windows 10 Home edition, so perhaps it is an OS problem?
It would be great if I could receive any advice or solutions.
Thank you
15 Comments
dpb
on 19 Sep 2018
I'm guessing the file was open when the session was started and that's when the check for being open occurred. Once the application discovered it couldn't access the file, it probably(?) terminates.
If you subsequently restart the process after closing the file, can it then be opened?
Eric Trudel
on 19 Sep 2018
dpb
on 19 Sep 2018
Unless there's some delay between closing the file and the ! command starting a session, I still suspect the OS hasn't got the tables updated yet to indicate the file is no longer open.
Try inserting a pause between and see if that doesn't alleviate the symptom.
Eric Trudel
on 19 Sep 2018
dpb
on 19 Sep 2018
"secondary script with no opening or closing of any files that simply calls the command string using system()"
Then it's hardly likely it is Matlab having the file locked that is the problem but more likely that the spawned process isn't operating in the location of the file or doesn't have the same environment variables set that tell the application where to search for files.
Try using a fully-qualified filename.
Eric Trudel
on 19 Sep 2018
OCDER
on 19 Sep 2018
Just curious, what's the exact script that doesn't work? Copy and paste it to the forum, and comment the section that generates the error.
Eric Trudel
on 19 Sep 2018
Edited: Eric Trudel
on 19 Sep 2018
OCDER
on 19 Sep 2018
"Note; Both the tcl script and the .fem file are in the same folder."
But according to your scripts, your tcl file is here:
"C:\Users\e-tru\Documents\test.tcl"
and your fem file is here:
"C:/Users/e-tru/Documents/Altair/Practice/Mult_objective_Stage_2_Practice/Large_Tension_Hinge_Rebuilt_2.2_Stage_2_Initial_1.fem"
Have you confirmed the location of the TCL file like this?
name_tcl ='test.tcl';
folder_path_tcl ='C:\Users\e-tru\Documents\';
path2 = fullfile(folder_path_tcl, name_tcl);
if ~exist(path2, 'file')
error('Could not find your TCL file at "%s".', path2');
end
command3 = strcat('"C:\Program Files\Altair\2017\hm\bin\win64\hmopengl.exe" -tcl ',' "',path2,'"');
system(char(command3));
Eric Trudel
on 19 Sep 2018
dpb
on 19 Sep 2018
"If I paste the system() command string into the command prompt; the tcl script can open hypermesh then find and upload the .fem files into the application. This does not happen if MATLAB spawned the process using system()."
That's the environment then almost certainly...check all environment variables in the command window in which you run interactively and then compare to those in the window when you just execute ! from Matlab.
Eric Trudel
on 20 Sep 2018
PATH isn't the same as starters--I didn't try to do precise comparison, but
>> p2='Path=C:\Program Files\MATLAB\R2017b\bin\win64;C:\MSC.Software\Patran_x64\20170\bin;C:\WINDOWS\system32;C:\WINDOWS;C:\WINDOWS\System32\Wbem;C:\WINDOWS\System32\WindowsPowerShell\v1.0\;C:\Program Files\MATLAB\R2017b\runtime\win64;C:\Program Files\MATLAB\R2017b\bin;C:\Program Files\Intel\WiFi\bin\;C:\Program Files\Common Files\Intel\WirelessCommon\;C:\WINDOWS\System32\OpenSSH\;C:\Program Files\Microsoft VS Code\bin;C:\Users\e-tru\AppData\Local\Programs\Python\Python37-32\Scripts\;C:\Users\e-tru\AppData\Local\Programs\Python\Python37-32\;C:\Users\e-tru\AppData\Local\Microsoft\WindowsApps;'
p2 =
'Path=C:\Program Files\MATLAB\R2017b\bin\win64;C:\MSC.Software\Patran_x64\20170\bin;C:\WINDOWS\system32;C:\WINDOWS;C:\WINDOWS\System32\Wbem;C:\WINDOWS\System32\WindowsPowerShell\v1.0\;C:\Program Files\MATLAB\R2017b\runtime\win64;C:\Program Files\MATLAB\R2017b\bin;C:\Program Files\Intel\WiFi\bin\;C:\Program Files\Common Files\Intel\WirelessCommon\;C:\WINDOWS\System32\OpenSSH\;C:\Program Files\Microsoft VS Code\bin;C:\Users\e-tru\AppData\Local\Programs\Python\Python37-32\Scripts\;C:\Users\e-tru\AppData\Local\Programs\Python\Python37-32\;C:\Users\e-tru\AppData\Local\Microsoft\WindowsApps;'
>> strcmpi(p1,p2)
ans =
logical
0
>> length(p1)
ans =
550
>> length(p2)
ans =
591
>>
Also, what is working directory between the two sessions?
Something in there somewhere isn't set up correctly when you're spawning a new session.
You might consider paring back to a "barebones" PATH and other environment that does work in the working session to use as a template for a batch file to dispatch to set the proper environment for the application.
What happens in Windows with "automagic" installers is adding a whole bunch of stuff behind the scenes you're not aware of when install a software package. You need to look at what the particular program needs, specifically, that has been taken care of for you by its installer or batch file or whatever that isn't done the other route.
Eric Trudel
on 21 Sep 2018
Adeline War
on 10 Mar 2022
Eric Trudel may I get your help in making a batch process between Matlab and HYpermesh?
Answers (0)
Categories
Find more on File Operations in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!