Detect P-code

조회 수: 10 (최근 30일)
Ronan
Ronan 2011년 12월 9일
Is there a way for a function to query if it is running from an m-file or if it has been deployed as a p-file. The function "isdeployed()" returns zero for p-code. Is there anything equivalent to "ispcode()"?
Thanks
Ronan
  댓글 수: 1
Daniel Shub
Daniel Shub 2011년 12월 10일
Why would you want to know if you are running pcode?

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

채택된 답변

Paulo Silva
Paulo Silva 2011년 12월 9일
Maybe this:
a=dbstack('-completenames');
if (isempty(strfind(a.file,'.m')))
disp('pcode')
else
disp('mfile')
end
  댓글 수: 3
Paulo Silva
Paulo Silva 2011년 12월 9일
Thanks Jan
a=dbstack('-completenames');
fend=a.file;
fend=fend(end-1:end);
if (strcmp(fend,'.m'))
disp('mfile')
elseif (strcmp(fend,'.p'))
disp('pcode')
else
disp('undefined')
end
Jan
Jan 2011년 12월 9일
See FEX: strncmpir :-)
This was developped exactly for this case. The "i" takes into account, that the file extension could be '.M' also.

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

추가 답변 (1개)

Jan
Jan 2011년 12월 9일
If a P-file is found in the path, it is used.
isPCode = (exist(mfilename, 'file') == 6);
But this is not fast. The best idea would be to modify the source before P-coding:
isPcode = false; % Toggled automatically
Then the P-coding:
fid = fopen(MFileName, 'r');
if fid < 0, error('Cannot open %s', MFileName); end
DataC = textscan(fid, '%s', 'delimiter', '\n');
fclose(fid);
Str = DataC{1};
Str = strrep(Str, 'isPcode = false;', 'isPcode = true;');
fid = fopen(MFileName, 'w');
if fid < 0, error('Cannot open %s', MFileName); end
fprintf(fid, '%s\n', Str{:});
fclose(fid);
pcode(MFileName, '-inplace');
And finally restore the original MFile again using DataC{1}. Unfortunately the modification date of the P-file is older than the M-file and a warning appears. This can be avoided either by setting the modification date of the P-file to a later time, or by saving the modified source to a temporary file only and call the builtin but undocumented _pwrite function, which allows to specify a different name for the P-file.
NOTE: I admit, this has an optimal runtime, but is not convenient.

카테고리

Help CenterFile Exchange에서 Programming에 대해 자세히 알아보기

제품

Community Treasure Hunt

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

Start Hunting!

Translated by