Detect P-code

10 visualizzazioni (ultimi 30 giorni)
Ronan
Ronan il 9 Dic 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 Commento
Daniel Shub
Daniel Shub il 10 Dic 2011
Why would you want to know if you are running pcode?

Accedi per commentare.

Risposta accettata

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

Accedi per commentare.

Più risposte (1)

Jan
Jan il 9 Dic 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.

Categorie

Scopri di più su Programming in Help Center e File Exchange

Prodotti

Community Treasure Hunt

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

Start Hunting!

Translated by