Effacer les filtres
Effacer les filtres

Can I run 2 or more matlab scripts (each running in a separate matlab session) using the same data in a folder without them interfering with each other?

8 vues (au cours des 30 derniers jours)
I have a set of database (multiple *.mat files) collected in a folder, which I can recall in a subsequent run. Let say I have n=100 mat files, and each *.mat file contains a set of matrices and vectors, say M and V. In such a subseq. run, computation will be performed for cases "n_i=1:100" subsequently, and M and V will be loaded for every "n_i". Would it be possible to run multiple scripts, each running a subsequent run? If script A is running n_i=3, and script B, running in the same folder, is running n_i=10, would M and V loaded by script A, replaced by the ones loaded by script B? Or are they compartmentalized in each script?
Thank you in advance.
  2 commentaires
Stephen23
Stephen23 le 19 Juin 2024
"Or are they compartmentalized in each script?"
Scripts are not "compartmentalized" at all. They are wild, uncontrolled, und unpredictable.
The solution is better code:
  • write functions (not scripts)
  • always LOAD into an output argument.
Simple steps that make your code much more robust, reliable, and repeatable.
Hendrawan Aji
Hendrawan Aji le 19 Juin 2024
thank you for your comment. I didn't know that I can use load into output argument. I'll keep that in mind.

Connectez-vous pour commenter.

Réponse acceptée

dpb
dpb le 19 Juin 2024
Déplacé(e) : dpb le 19 Juin 2024
If they are started in separate sessions, they know nothing of each other.
In a script within the same session, variables are common so indeed loading a second .mat file containing variables of the same name would replace the existing variables.
There are two ways around this--
  1. Use the functional form of the load command and return the data in a struct array that can be referenced, or
  2. Use functions instead of scripts so all variables are local within the function.
The scenario described of multiple sessions would be better servied with the parallel toolbox, however.
  3 commentaires
dpb
dpb le 19 Juin 2024
Depending on the file system characteristics, running multiple sessions against the same set of datafiles may occasionally cause access issues of one process having the file open when another tries to access it. Without checking via a catch...try...end block or somesuch, MATLAB will just fail and terminate if a file cannot be opened. How likely this is to occur will be very depenent upon just how long any given process has a file open; if it is just reading a small file, you may never see an issue.
And, of course, if there's just one copy of each of these in a shared location, if your processing app modifies the file, then all processes will subsequently have that modified file, not their original.
Hendrawan Aji
Hendrawan Aji le 20 Juin 2024
I'll keep that in mind. The matrices are about 20-100 Gb each, but they will only be recalled (load) and not modified during the subseq. runs. I'll put a few minutes or an hour distance between them so to avoid accessing the same matrix at the same time.

Connectez-vous pour commenter.

Plus de réponses (0)

Catégories

En savoir plus sur Reporting and Database Access dans Help Center et File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!

Translated by