Detect P-code
조회 수: 4 (최근 30일)
이전 댓글 표시
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
채택된 답변
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
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
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
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.
댓글 수: 0
참고 항목
제품
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!