Saving a file in a relative directory on the current .m file.

54 vues (au cours des 30 derniers jours)
Zarif Rahman
Zarif Rahman le 18 Fév 2021
Commenté : Zarif Rahman le 19 Fév 2021
I am having trouble setting up a save file folder. It just seems to save my file to whatever my current directory is. Is it possible to save in a certain location relative to the .m file that I run. The file that I run is deep withing my folder structure, and I want to save it a folder back with current date. I tried using mkdir() but that would just make folders anywhere my current path was. I also tried using CD to my path location but it just seems to be ignored and it starts saving anywhere. One thing that I also see is repeating folder structures where my code basically recreates the entire two folders within my directory. Any solution to this Problem?

Réponse acceptée

Cris LaPierre
Cris LaPierre le 19 Fév 2021
Then use an absolute path. You can use fullfile for that. If it needs to be relative to the m-file, you can use the following syntax of which
to get the path of the m file. Then use that as an input to fullfile to build up the desired save path. This assumes your m file is on your path.

Plus de réponses (1)

Cris LaPierre
Cris LaPierre le 18 Fév 2021
Yes, relative paths are possible
  • ./folder/file.m save path begins in the current folder
  • ../folder/file.m save path begins in the parent folder (up one level)
  • ../../folder/file.m save path begins in the parent of the parent folder (up two levels)
  9 commentaires
Zarif Rahman
Zarif Rahman le 19 Fév 2021
Can I make sure your comment with the which keyword is the solution to the question I asked here. I feel this will help people who are having the same issue.
Cris LaPierre
Cris LaPierre le 19 Fév 2021
I'm moved it to a new answer. You can move your comments there as well and then accept that as the best answer.

Connectez-vous pour commenter.

Catégories

En savoir plus sur File Operations dans Help Center et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by