- write functions (not scripts)
- always LOAD into an output argument.
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?
3 次查看(过去 30 天)
显示 更早的评论
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 个评论
Stephen23
2024-6-19
"Or are they compartmentalized in each script?"
Scripts are not "compartmentalized" at all. They are wild, uncontrolled, und unpredictable.
The solution is better code:
Simple steps that make your code much more robust, reliable, and repeatable.
采纳的回答
dpb
2024-6-19
移动:dpb
2024-6-19
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--
- Use the functional form of the load command and return the data in a struct array that can be referenced, or
- 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 个评论
dpb
2024-6-19
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.
更多回答(0 个)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Reporting and Database Access 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!