Detect P-code

10 visualizaciones (últimos 30 días)
Ronan
Ronan el 9 de Dic. de 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 comentario
Daniel Shub
Daniel Shub el 10 de Dic. de 2011
Why would you want to know if you are running pcode?

Iniciar sesión para comentar.

Respuesta aceptada

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

Iniciar sesión para comentar.

Más respuestas (1)

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

Categorías

Más información sobre Programming en Help Center y File Exchange.

Productos

Community Treasure Hunt

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

Start Hunting!

Translated by