How can I access a private folder with a MATLAB Function block in Simulink R2022a?
7 visualizzazioni (ultimi 30 giorni)
Mostra commenti meno recenti
MathWorks Support Team
il 9 Mag 2023
Risposto: MathWorks Support Team
il 15 Mag 2023
In the MATLAB editor, I am able to access private folders, which are not on the current path, with scripts and functions in the parent folder. For example, I have a parent folder with files as seen below.

The file 'parentFunction.m' has the following code to call 'matlab.mat', which is stored in the private folder.
function out = parentFunction()
data = load('matlab.mat');
out = data.a;
end
This function is able to successfully load 'matlab.mat' from the private folder. However, when attempting the same implementation in a 'MATLAB Function' block, this is not the case. The model 'parentModel.slx' is constructed as shown.

And the function inside the block is as follows.
function y = fcn()
y = parentFunction();
end
When this model is run, the following error occurs.
Failed to load file 'matlab.mat': 'matlab.mat' is not found in the current folder or on the MATLAB path
I thought that the 'MATLAB Function' block has the same functionality as a script within the MATLAB editor. Is there a workaround that allows the block to access private folders?
Risposta accettata
MathWorks Support Team
il 9 Mag 2023
The 'MATLAB Function' block should work the same as a MATLAB script. Because of this, private folders can be used to provide private functions only up one level to the public function level, a limitation that also exists within MATLAB scripts. So in addition to a MAT file, a function file should be placed in the private folder which can be coded to load the MAT file. For example, a function file "doload.m" can be added to the folder "private" which has the following code to load the MAT file.
function out = doload()
data = load('private/matlab.mat');
out = data.a;
end
This function will then be called by the public function 'parentFunction.m', as seen here.
function out = parentFunction()
out = doload();
end
Now, the function in the 'MATLAB Function' block will execute properly to access the private folder. It is important to recognize that the 'doload.m' function itself will operate on the MATLAB path directly without noting the private location, just like it does in MATLAB, so the file path to the private folder must be specified within the function added to the private folder.
0 Commenti
Più risposte (0)
Vedere anche
Categorie
Scopri di più su Simulink Functions in Help Center e File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!