Hiding workspace values in Pcode
조회 수: 7 (최근 30일)
이전 댓글 표시
Hello All, I am trying to distribute my code as protected P code. The only thing I am not happy after conversion is the variables used in my codes are still visible under workspace.
Is there any way so that, once converted to P code, the workspace will be blank? same as that of .exe ?
댓글 수: 0
답변 (1개)
Guillaume
2018년 2월 28일
Write a function instead of a script. It's better design anyway and functions execute faster than scripts. More importantly, the workspace of a function is destroyed when the function terminates.
댓글 수: 9
Walter Roberson
2018년 3월 1일
function your_entry_point
z = calculations(inputs());
disp(z);
function s1 = inputs()
p1='Provide input for m = ';
s1.m=input(p1);
p2='Provide input for n = ';
s1.n=input(p2);
.....
....
function z = calculations(s1)
z=s1.m*s2.m;
Guillaume
2018년 3월 1일
As Walter said, your original code doesn't make sense. It looks like something that is part of a GUI.
My advice for structuring your code is that your main function should not ask the user for inputs. Hence, no input, inputdlg. It should be a simple function with standard input and output parameters, e.g.:
function result = your_main_function(m, n)
result = dosomecalcwithm_and_n
end
You can p-code that, the user will never know what calculations you do with m and n and will only see the result. Why do it like that? Because that function can then be called multiple times (e.g. in a loop) by any code the user write without requiring user interaction. It's up to them to provide the required input in any way they want rather than you forcing them to write them at command prompt. They can write code to fetch the inputs from a database and pass them to your function if they so wish. Having an input in your code would prevent that.
In parallel, you can provide a convenience function (or script) that uses input and call your main function. It does not even need to be p-coded since it doesn't reveal anything about the inner working of your code:
function input_helper
m = input('Provide input for m = ');
n = input('Provide input for n = ');
result = your_main_function(m, n);
disp('The result is');
disp(result);
end
참고 항목
카테고리
Help Center 및 File Exchange에서 Startup and Shutdown에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!