Hiding workspace values in Pcode

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 ?

Answers (1)

Guillaume
Guillaume on 28 Feb 2018
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 Comments

well I am trying to convert my script into function. I have converted inputs section as inputs.m function file. and calculation are kept in calculation.m file. Now thing is, I am not able to share the variables from input function to calculations function.
Here is what I did in inputs.m file to share the variables:
function inputs(hObject,eventdata,handles)
p1='Provide input for m = ';
s1.m=input(p1);
p2='Provide input for n = ';
s1.n=input(p2);
.....
....
....
set(handles.inputs,'UserData',s1);
now in calculations.m file i have:
function calculations()
s1 = get(handles.inputs, 'UserData');
z=s1.m*s2.m;
I am sure I have messed up something here.need guidance!
That looks like it would work, provided that calculations is never called until after inputs has been called at least once.
in my mainScript.m I have following code:
clc;
inputs;
calculations;
And I am facing this error:
Error using inputs (line 22)
Not enough input arguments.
Where 22nd line is:
set(handles.inputs,'UserData',s1);
You have coded inputs as a callback, something to be invoked when the user clicks on something particular or moves the mouse or presses a key. Your routines are not coded to be called directly by your program. It is possible to call directly but are you sure you want to?
I am not sure what wrong I did there. My only intention is to hide my variables. to do this, I am converting my plain script into function files. now I have converted 1 script into 3. 1)inputs file which will take inputs from user from command window. 2)calculations file which uses user inputs and do the calculations. 3)mainScript which calls inputs and calculations file.
So I want to make it work. so I am a bit unsure how would I share my variables from inputs function file to be used in calculations file.
Return them as a struct from inputs(). Pass the struct to calculations().
You should avoid hObject and event and (probably) handles unless you are writing a GUI. For one thing, anything that you put into handles will still be in handles after you return from your entry function, unless you take the time to purge the entries. And if you put anything into handles and the user happens to have code that manages to interrupt (a timer for example) then the values will be sitting there in handles for them to look at... which kind of weakens the point of pcode.
can you give a simple example?
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;
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

Sign in to comment.

Categories

Find more on Loops and Conditional Statements in Help Center and File Exchange

Tags

Asked:

on 28 Feb 2018

Commented:

on 1 Mar 2018

Community Treasure Hunt

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

Start Hunting!