Unable to create file when using pyrunfile

3 Ansichten (letzte 30 Tage)
鑫
am 4 Jan. 2024
Bearbeitet: Hassaan am 4 Jan. 2024
I run a.py in python and the result produces a number of .mat files
file_name = f'temp{execution_count}.mat'
scipy.io.savemat(file_name, {'x_best': a, 'value': b, 'Name': c, 'count': execution_count}),
But when I run a.py in matlab using pyrunfile, these files are not produced
pyrunfile("C:\Users\yx\Desktop\a.py");
I searched the entire hard drive and tried to run it again after changing the absolute path to scipy.io.savemat, neither of which found the corresponding files, so I'm guessing that the files are not being stored.
I've run other code with pyrunfile before with no problems, so I don't think it's a problem with the environment configuration.

Antworten (1)

Hassaan
Hassaan am 4 Jan. 2024
Bearbeitet: Hassaan am 4 Jan. 2024
  1. Current Working Directory: The MATLAB and Python environments may have different current working directories. The Python script will save the .mat files in its current working directory, which might not be the same as the MATLAB current working directory.
  2. File Path: The file path you're using in Python ('temp{execution_count}.mat') is relative. If the working directory is not what you expect, the files may be saved elsewhere. Always use absolute paths to avoid such confusion.
  3. Permissions: There might be permissions issues. Ensure that the Python environment called by MATLAB has the necessary permissions to write files to the desired directory.
  4. Environment Variables: If the Python script relies on environment variables that are set in your regular Python environment, those variables may not be available when the script is run from within MATLAB.
  5. Execution Count Variable: Make sure that the execution_count variable in your Python code is defined and being incremented as expected. If there's an issue with this variable, it could prevent the filenames from being constructed correctly.
Here are a few steps to troubleshoot:
  • Check Current Working Directory: In MATLAB, check the current working directory using pwd. In the Python script, you can print the working directory at runtime by adding print(os.getcwd()) and import os at the beginning of your script.
  • Use Full Paths: Modify the Python script to save the .mat files to an absolute path that you specify.
  • Check for Exceptions: Modify the Python script to catch and print exceptions that may occur during file writing.
  • Run Python Script Independently: Run the Python script outside of MATLAB to ensure it's working as expected in isolation.
Here's an example of how you might modify the Python script to print the current working directory and use an absolute path for the files:
import os
import scipy.io
# Print the current working directory
print('Current working directory:', os.getcwd())
# Use an absolute path for the file
file_name = f'C:/absolute/path/to/temp{execution_count}.mat'
# Try to save the file and catch any exceptions
try:
scipy.io.savemat(file_name, {'x_best': a, 'value': b, 'Name': c, 'count': execution_count})
print(f'File saved: {file_name}')
except Exception as e:
print(f'Error saving file: {e}')
In MATLAB, when calling pyrunfile, make sure to specify the correct path to your Python script:
pyrunfile('C:\Users\yx\Desktop\a.py');
This will help to identify where the issue might be occurring. If the files are being saved but not where you expect, the printed working directory will help you locate them. If there's an error during saving, the exception message will provide more details.
------------------------------------------------------------------------------------------------------------------------------------------------
If you find the solution helpful and it resolves your issue, it would be greatly appreciated if you could accept the answer. Also, leaving an upvote and a comment are also wonderful ways to provide feedback.
Professional Interests
  • Technical Services and Consulting
  • Embedded Systems | Firmware Development | Simulations
  • Electrical and Electronics Engineering

Kategorien

Mehr zu Call Python from MATLAB finden Sie in Help Center und File Exchange

Produkte


Version

R2022b

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by