Discover MakerZone

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn more

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today

Thread Subject:
Function preceding order within a deployed application

Subject: Function preceding order within a deployed application

From: Bruno Luong

Date: 24 Apr, 2013 10:46:09

Message: 1 of 3

I would like to know if the following specification
http://www.mathworks.fr/fr/help/matlab/matlab_prog/function-precedence-order.html

is applied for deployed code (using MATLAB compiler) as well?

On my test, the "Functions in the current folder" does not seem to shadow the hononyme function(s) for deployed application.

I would like to find a mechanism of shadowing function (as with non-deployed matlab) for deployed application, which works well under the compiler 2006B.

I don't find any relevant doc on the subject, beside Loren's blog (not quite a official reference spec to me).

Here is a minima example code, which comprises 3 files ./foo.m, ./subfolder1/subfun.m and ./subfolder2/subfun.m

function foo(varargin)
% function foo(varargin)
% mcc -C -m foo.m -a subfolder1/subfun.m -a subfolder2/subfun.m
% Minima example to illustrate the 'Function preceding order' document
% http://www.mathworks.fr/fr/help/matlab/matlab_prog/function-precedence-order.html
% is not applicable in deployed code (2013A)

%% First path
path1 = locpath('subfolder1');
cd(path1);
addpath(path1,'-begin');
rehash path;
fun1 = @subfun;
fun = functions(fun1);
disp(fun.file);
feval(fun1);

%% Second path
path2 = locpath('subfolder2');
cd(path2);
addpath(path2,'-begin');
rehash path;
fun2 = @subfun;
fun = functions(fun2);
disp(fun.file);
feval(fun2);

cd(locpath(''));
rmpath(path1);
rmpath(path2);

input('foo>> Type CR to quit: ', 's' );

end % foo

%%
function path = locpath(relpath)
% local function declared and used by in foo.m
% return the absolute path from the relative path
if isdeployed()
    root = ctfroot();
else
    root = fileparts(mfilename('fullpath'));
end
path = [root filesep() relpath];
end % locpath

%%
function subfun( )
% subfun( )
% A dummy function located in subfolder1/subfun
fprintf('subfun in folder 1 call\n');
end % subfun

%%
function subfun( )
% subfun( )
% A dummy function located in subfolder2/subfun
fprintf('subfun in folder 2 call\n');
end % subfun

%%%%%%%%%%%%%%
If I run under MATLAB, I get this output:

>> foo
C:\Somewhere\subfolder1\subfun.m
subfun in folder 1 call
C:\Somewhere\subfolder2\subfun.m
subfun in folder 2 call


When running wth stand alone deployed application I get this.

C:\Somewhere> foo.exe

C:\Somewhere\foo_mcr\subfolder2\subfun.m
subfun in folder 2 call
C:\Somewhere\foo_mcr\subfolder2\subfun.m
subfun in folder 2 call

There is no way to shadow subfun in subfolder2. Is there any work around?

Bruno

Subject: Function preceding order within a deployed application

From: Bruno Luong

Date: 27 Apr, 2013 09:13:09

Message: 2 of 3

Follow up: I have reported the issue to TMW support, and get a workaround.

All the shadowing paths must be removed first, then specific path will be added before subfunctions in that folder are called. Why [in deployed mode] removing paths is needed, but calling addpath() alone is not sufficient is beyond my understanding.

When things like that happens, thought that MATLAB is obscurely oddly designed SW certainly cross my mind.

Here is a modfied foo.m that works:

%%%%
function foo(varargin)

path1 = locpath('subfolder1');
path2 = locpath('subfolder2');
rmpath(path1);
rmpath(path2);

addpath(path1,'-begin');
fun1 = @subfun;
fun = functions(fun1);
disp(fun.file);
feval(fun1);

addpath(path2,'-begin');
fun2 = @subfun;
fun = functions(fun2);
disp(fun.file);
feval(fun2);

input('foo>> Type CR to quit: ', 's' );

end % foo

%
function path = locpath(relpath)
% local function used by in foo.m
% return the absolute path from the relative path
if isdeployed()
    root = ctfroot();
else
    root = fileparts(mfilename('fullpath'));
end
path = [root filesep() relpath];
end % locpath

% Bruno

Subject: Function preceding order within a deployed application

From: Bruno Luong

Date: 30 Apr, 2013 11:19:09

Message: 3 of 3

For those who care about speed (like us) the above workaorund using ADDPATH and RMPATH is quite slow compared to CD. About 20 time slower on PC platform.

Bruno

Tags for this Thread

No tags are associated with this thread.

What are tags?

A tag is like a keyword or category label associated with each thread. Tags make it easier for you to find threads of interest.

Anyone can tag a thread. Tags are public and visible to everyone.

Contact us