Skip to content
GitLab
Explore
Sign in
Register
Primary navigation
Search or go to…
Project
P
PythonControl
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Iterations
Wiki
Requirements
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Locked files
Build
Pipelines
Jobs
Pipeline schedules
Test cases
Artifacts
Deploy
Releases
Package registry
Container registry
Model registry
Operate
Environments
Terraform modules
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Code review analytics
Issue analytics
Insights
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
GitLab community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
PA
Operation
PythonControl
Commits
7100a9de
Commit
7100a9de
authored
Jun 5, 2024
by
Alexis GAMELIN
Browse files
Options
Downloads
Patches
Plain Diff
Add function to send commands to matlab from python
parent
49d983cf
Branches
Branches containing commit
Tags
Tags containing commit
No related merge requests found
Changes
2
Show whitespace changes
Inline
Side-by-side
Showing
2 changed files
.gitignore
+1
-0
1 addition, 0 deletions
.gitignore
matlab.py
+59
-0
59 additions, 0 deletions
matlab.py
with
60 additions
and
0 deletions
.gitignore
0 → 100644
+
1
−
0
View file @
7100a9de
*.pyc
This diff is collapsed.
Click to expand it.
matlab.py
0 → 100644
+
59
−
0
View file @
7100a9de
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Created on Wed Jun 5 14:06:14 2024
@author: gamelina
"""
import
subprocess
import
os
from
pathlib
import
Path
def
__write_mat_file
(
matlab_command
):
path
=
Path
(
os
.
getcwd
())
/
"
temp_matlab_file.m
"
with
open
(
path
,
"
w
"
)
as
file
:
file
.
write
(
"
cd(getenv(
'
MLROOT
'
));
"
)
file
.
write
(
'
\n
'
)
file
.
write
(
"
setpathsoleil(
'
StorageRing
'
);
"
)
file
.
write
(
'
\n
'
)
file
.
writelines
(
matlab_command
)
file
.
write
(
'
\n
'
)
file
.
write
(
"
quit force
"
)
return
path
def
to_matlab
(
matlab_command
,
remove_temp_file
=
True
,
verbiose
=
False
):
"""
Send command to be exectued in matlab from python.
Parameters
----------
matlab_command : str or array-like of str
Matlab commands to be exectued.
If array-like (list, arrays, ...) then each line must be separeted by
'
\n
'
.
remove_temp_file : bool, optional
If True, delete matlab temporay file at the end of function.
The default is True.
verbiose : bool, optional
Print out content of matlab file and bash command.
The default is False.
Returns
-------
None.
"""
temp_file
=
__write_mat_file
(
matlab_command
)
if
verbiose
:
print
(
"
matlab script:
"
)
with
open
(
temp_file
,
'
r
'
)
as
file
:
print
(
file
.
read
())
string
=
f
"'
{
str
(
temp_file
)
}
'"
command
=
f
'
/home/configuration/bin/matlab2018b -nosplash -r
"
run(
{
string
}
); exit;
"'
if
verbiose
:
print
(
"
bash command:
"
)
print
(
command
)
subprocess
.
call
(
command
,
shell
=
True
)
# this actually wait for matlab to close
if
remove_temp_file
:
os
.
remove
(
temp_file
)
\ No newline at end of file
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment