How can I access a private folder with a MATLAB Function block in Simulink R2022a?

7 views (last 30 days)
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?

Accepted Answer

MathWorks Support Team
MathWorks Support Team on 9 May 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.

More Answers (0)

Categories

Find more on Simulink Functions in Help Center and File Exchange

Products


Release

R2022a

Community Treasure Hunt

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

Start Hunting!