You are now following this question
- You will see updates in your followed content feed.
- You may receive emails, depending on your communication preferences.
Matlab prevents external program from accessing a file when Matlab script is running
1 view (last 30 days)
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
Thanks for the response, the file is closed before calling the command prompt. The issue is that an error occurs when I call Hypermesh from the command prompt using system() in matlab. If I paste the string that would be called by system() into the command prompt, Hypermesh opens and the script functions as expected with no errors.
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
I added a pause but that did not seem to prevent any errors. I wrote a secondary script with no opening or closing of any files that simply calls the command string using system() . However, MATLAB is still preventing my Hypermesh script from accessing the .fem file.
Is there a way to unlock a file using MATLAB? or by using the command prompt?
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
I have always been using fully-qualified filenames for locating files, both in MATLAB and my tcl script. 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().
Here is what I have tried;
1. Command string in the command prompt executes the tcl script with no problem.
2. TCL script pasted into the TCL console in Hypermesh has no problems.
3. TCL script run as a script in Hypermesh has no problems.
4. Command string called by MATLAB prevents Hypermesh from accessing certain files. Hypermesh is also unable to upload other files into the gui manually, unless the MATLAB script finishes running.
Note; Both the tcl script and the .fem file are in the same folder.
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
Here is the matlab script:
%testing
%---%TCL 2 Filename%---%
name_tcl="test.tcl";
folder_path_tcl="C:\Users\e-tru\Documents\";
path2=strcat(folder_path_tcl,name_tcl);
%run command from command prompt, hypermesh opens, then the script begins,
%error occurs when the script wants to upload a .fem file (and a .sh file)
command3=strcat('"C:\Program Files\Altair\2017\hm\bin\win64\hmopengl.exe" -tcl ',' "',path2,'"');
system(char(command3)); %problems start from this line
The TCL script; the error will occur when I use the hypermesh function *ossmooth to upload the needed files. If there is an error, the try-catch statement will exit hypermesh. The problem exists when the script is called from system() in MATLAB (from my understanding):
#threshold values
set solid_domain_threshold 0.700000
set lattice_domain_threshold [expr 1 - $solid_domain_threshold]
#osmooth constrols
set surfacecode 2
set laplacian_iteration 20
set laplacian_feature_angle 25
#"isosurf: surfacecode1 surfacemode2 densitythreshold3 detectbridge4 lowthreshold5 distancecoefficient6 optismoothboundary7 lapiteration8 lapfeatureangle9 lapsmoothboundary10 drawrecovery11"
puts "Uploading Solid Domain"
*createstringarray 2 "isosurf: 3 3 $solid_domain_threshold 1 -1 0 0 10 30 1 1" "other_params: 1 0 0 0 10 0"
###########################################
##ERROR occurs in the line below###########
###########################################
if { [ catch {*ossmooth_12 0 1 0 1 "C:/Users/e-tru/Documents/Altair/Practice/Mult_objective_Stage_2_Practice/Large_Tension_Hinge_Rebuilt_2.2_Stage_2_Initial_1.fem" "C:/Users/e-tru/Documents/Altair/Practice/Mult_objective_Stage_2_Practice/Large_Tension_Hinge_Rebuilt_2.2_Stage_2_Initial_1.sh" "*.grid" 1 0 1 2 } ] } {
puts "Error During Solid Domain Tetmeshing: Refining Mesh"
#exit the console
hm_answernext yes
*quit 1;
hm_answernext yes
} else {
puts "Solid Domain TetMesh Succesful!"
}
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
I have two tcl scripts; a small one for debugging the section that generates errors and another main script that contains more code. The small script is called "test.tcl". However the small and large scripts generate the same error when executed from MATLAB.
I checked if both the .tcl and .fem files exist, and they return no errors (they exist).
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
I checked my environment variables, both within the command prompt and MATLAB, but I don't understand what to look for. The only differences that exists when MATLAB calls the command prompt are the following environment variables below:
KMP_BLOCKTIME=1 KMP_HANDLE_SIGNALS=0 KMP_STACKSIZE=512k KMP_REGISTERED_LIB_15500=00007FFDC9DD7954-cafe3697-libiomp5md.dll
I uploaded a csv file that contains all environment variables for comparison.
A path was also added to the PATH variable that links to the folder with all my files but that did not fix the problem.
dpb
on 20 Sep 2018
Edited: dpb
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
Alright, turns out the problem was that the working directory changed when I called Hypermesh from MATLAB (went to C:/Windows/System32 not C:/Users/e-tru/Documents ). So I had to change my working directory through my tcl script to C:/Users/e-tru/Documents. Now the script works as intended.
Thank you dpb and OCDER for your help.
Adeline War
on 10 Mar 2022
Eric Trudel may I get your help in making a batch process between Matlab and HYpermesh?
Answers (0)
See Also
Categories
Find more on Matrix Indexing 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!An Error Occurred
Unable to complete the action because of changes made to the page. Reload the page to see its updated state.
Select a Web Site
Choose a web site to get translated content where available and see local events and offers. Based on your location, we recommend that you select: .
You can also select a web site from the following list
How to Get Best Site Performance
Select the China site (in Chinese or English) for best site performance. Other MathWorks country sites are not optimized for visits from your location.
Americas
- América Latina (Español)
- Canada (English)
- United States (English)
Europe
- Belgium (English)
- Denmark (English)
- Deutschland (Deutsch)
- España (Español)
- Finland (English)
- France (Français)
- Ireland (English)
- Italia (Italiano)
- Luxembourg (English)
- Netherlands (English)
- Norway (English)
- Österreich (Deutsch)
- Portugal (English)
- Sweden (English)
- Switzerland
- United Kingdom (English)
Asia Pacific
- Australia (English)
- India (English)
- New Zealand (English)
- 中国
- 日本Japanese (日本語)
- 한국Korean (한국어)