Call a pcode file (of a character array) within a MatLab script

1 view (last 30 days)
Im trying to call a pcode file whose contents are a character array as part of a MatLab .m file script and compare the contents of the pcode file to a non pcode file whose contents are also a character array. If I call the pcode file in the command window it displays the contents and puts it in the workspace within the variable "ans". However when I call the pcode file in the .m file script it does not appear to actually run the pcode file and the "ans" in the workspace is "0". I am calling it by just using the filename of the pcode without the ".p" in the MatLab script.

Accepted Answer

Walter Roberson
Walter Roberson on 17 Feb 2023
This is unusual, but possible. Code can examine the call-stack depth to determine whether it was called inside a function or script, or from the command line.
It would be more common to have code that produces different results depending on whether it called in an output context or not. For example,
fprintf('hello')
hello
ans
0 + fprintf('hello')
hello
ans = 5
ans
ans = 5
What is happening here is that fprintf() is examining nargout() and only generating return values in the case that nargout == 1.
Or consider the difference between
plot(1:5)
ans
ans = 5
plot noticed that no output was requested and so did not emit any output
h = plot(1:5)
h =
Line with properties: Color: [0 0.4470 0.7410] LineStyle: '-' LineWidth: 0.5000 Marker: 'none' MarkerSize: 6 MarkerFaceColor: 'none' XData: [1 2 3 4 5] YData: [1 2 3 4 5] Show all properties
plot() noticed that output was requested and returned the line() handles.
So code can pay attention to whether output is requested or not.
Anyhow... I would suggest experimenting with using evalc() and see what you get in the output variable.
  1 Comment
mark hoffman
mark hoffman on 18 Feb 2023
Thank you! I ran the evalc() on the pcode in the MatLab script and the workspace variable I assigned to evalc() within the MatLab script was populated by the pcode file contents after "ans = " so I think I can play around with this to isolate the actual answer and remove the unwanted characters (ans = ).

Sign in to comment.

More Answers (0)

Products


Release

R2022a

Community Treasure Hunt

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

Start Hunting!