I need to run a function from a software package that is written in a .p file. To run it on a cluster, I need to start matlab with the following options:
matlab -nojvm -nodesktop -nosplash -nodisplay -singleCompThread
The .p file attempts to make figures which cause matlab to crash due to nojvm. Is there anyway to prevent the figure creation without being able to edit the source code?
Thanks in advance!

댓글 수: 7

It should not work, but you can try something like this:
function x=figure(varargin), x = 1+rand; end
Fangjun Jiang
Fangjun Jiang 2022년 9월 22일
I doubt this would work. What about the usual plot() commands after the figure()? But you could try and report what happened.
Rik
Rik 2022년 9월 22일
It depends on how and when the p file determines which function to run. I personally think it should give preference to built-in functions, but I never tested it. This seems the easiest attack vector other than modifying the actual internal function (which is probably very much non-trivial in the case of figure).
Oliver Warrington
Oliver Warrington 2022년 9월 22일
Thanks for the suggestion @Rik. No luck though I'm afraid. Seems there is preference for built-in functions. Unfortunate for this case but I'm sure reasonable overall.
When I test with my own .p and my own replacement figure(), the replacement does get called.
S = "function test_pcode; fprintf('function called!\n'); fig = figure(); dfig = double(fig); fprintf('figure number was %d\n', dfig); end"
fid = fopen('test_pcode.m', 'w');
fwrite(fid, S);
fclose(fid)
which test_pcode
pcode test_pcode
which test_pcode
clear figure
which figure
test_pcode
function fignum = figure(varargin)
fprintf('replacement figure called!\n');
fignum = randi(20);
end
When I test this code here in Answers, the replacement figure message does not get displayed -- it is calling the built-in figure() it appears. But when I test the code in R2022b rerelease on my desktop, the message does appear indicating that my replacement function was called.
Rik
Rik 2022년 9월 23일
The whole point of p code is to prevent the end user digging around, so perhaps the writer of the code used the builtin() function to ensure built-in functions are called.
The inconsistent behavior is odd. I wonder whether it has something to do with the OS.
Walter Roberson
Walter Roberson 2022년 9월 23일
My guess at the moment would be that it has to do with Answers running in a LiveScript environment. LiveScript must be implicitly intercepting figure() so that it can inline figures.

댓글을 달려면 로그인하십시오.

답변 (1개)

Oliver Warrington
Oliver Warrington 2022년 9월 23일

1 개 추천

I did get this to work by using a brute force version of replacing the built in functions, as suggested in the comments.
I created dummy versions of the graphics, graph2d, graph3d and specgraph folders. Replaced all the functions, so no subsequent plot call of any kind would throw an error. Then removed the built in versions from the path and added my dummy versions.
rmpath(genpath('/usr/local/MATLAB/R2021b/toolbox/matlab/graph2d'))
rmpath(genpath('/usr/local/MATLAB/R2021b/toolbox/matlab/graphics'))
rmpath(genpath('/usr/local/MATLAB/R2021b/toolbox/matlab/specgraph'))
rmpath(genpath('/usr/local/MATLAB/R2021b/toolbox/matlab/graph3d'))
addpath(genpath('./graphics'));
addpath(genpath('./graph2d'));
addpath(genpath('./specgraph'));
addpath(genpath('./graph3d'));
Not sure this would be recommended in general, but my results are as expected and I can now run it on a cluster!

댓글 수: 1

Rik
Rik 2022년 9월 23일
I'm sure I would not recommend this in general, but in your case it seems the only feasible solution.

댓글을 달려면 로그인하십시오.

카테고리

도움말 센터File Exchange에서 Creating, Deleting, and Querying Graphics Objects에 대해 자세히 알아보기

제품

릴리스

R2018b

태그

질문:

2022년 9월 22일

댓글:

Rik
2022년 9월 23일

Community Treasure Hunt

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

Start Hunting!

Translated by