Detect P-code

10 Ansichten (letzte 30 Tage)
Ronan
Ronan am 9 Dez. 2011
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 Kommentar
Daniel Shub
Daniel Shub am 10 Dez. 2011
Why would you want to know if you are running pcode?

Melden Sie sich an, um zu kommentieren.

Akzeptierte Antwort

Paulo Silva
Paulo Silva am 9 Dez. 2011
Maybe this:
a=dbstack('-completenames');
if (isempty(strfind(a.file,'.m')))
disp('pcode')
else
disp('mfile')
end
  3 Kommentare
Paulo Silva
Paulo Silva am 9 Dez. 2011
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 am 9 Dez. 2011
See FEX: strncmpir :-)
This was developped exactly for this case. The "i" takes into account, that the file extension could be '.M' also.

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (1)

Jan
Jan am 9 Dez. 2011
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.

Kategorien

Mehr zu Programming finden Sie in Help Center und File Exchange

Produkte

Community Treasure Hunt

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

Start Hunting!

Translated by