call one code as a function in another code

1 Ansicht (letzte 30 Tage)
dhivya
dhivya am 23 Mär. 2023
Kommentiert: Vilém Frynta am 25 Mär. 2023
S=imread('text input.png');
meven=[0 0 0;
0 1 1;
1 0 1;
1 1 0];
modd=[0 0 1;
0 1 0;
1 0 0;
1 1 1];
no=1;
y =zeros(size(S));
R=zeros(size(S));
l=zeros(size(S));
for i = 1:size(S,1)
for j = 1:size(S,2)
if (S(i, j) == 0)
r=rand([no,2^no-1]);
disp(r)
ro=round(r*(size(meven,1)-1))+1;
disp(ro)
R(i,j)=meven(ro,1);
l(i,j)=meven(ro,2);
else
r=rand([no,2^no-1]);
disp(r)
ro = round(r*(size(modd,1)-1))+1;
disp(ro)
R(i,j)=modd(ro,1);
l(i,j)=modd(ro,2);
end
end
end
y(i,j)=S(i,j);
hii friends,this is algorithm 2 code..i named this as algo2 and the output of the algo2 is two share pixels that come out correctly.And i want to call this algorithm as a function in another algorithm.Here the code of another algorithm
C=imread('text.png');
S=imread('binary.png');
[m, n] = size(S);
no=2;
beta=1;
R1 = zeros(m, n);
R2 = zeros(m, n);
for i = 1:m
for j = 1:no
d = rand;
if d < beta
T=algo2(S(i,j),n);
R1(i, j) = T(1);
R2(i, j) = T(2);
else
if mod(n*C(i, j), no) == 0
R1(i, j) = C(i, j);
R2(i, j) = C(i, j);
else
f = randi([1 no]);
for k = 1:n
if k == f
R1(i, j) = 1 - C(i, j);
R2(i, j) = 1 - C(i, j);
elseif k < f
R1(i, j) = C(i, j);
R2(i, j) = C(i, j);
else
R1(i, j) = C(i, j);
R2(i, j) = C(i, j);
end
end
end
end
end
end
if i call the algo2 as a function in above code it shows error like:Execution of script algo2 as a function is not supported:
/MATLAB Drive/algo2.m
Error in threeexam (line 12)
T=algo2(S(i,j),n);
for this error i add function in algo2 but it shows error if i add functions in algo2.
please help me to rectify it.

Antworten (1)

Vilém Frynta
Vilém Frynta am 23 Mär. 2023
Bearbeitet: Vilém Frynta am 23 Mär. 2023
hey,
if you want to use your script as a function, you need few things.
  1. it seems like you are not familiar with creating function, so i will recommend taking a look at the documentation: function.
  2. create a new script, which will contain function only. the name of the script should be the same as the name of function, for example, your function will be named "algo2", your script should be named "algo2.m".
  3. paste your code there and define input arguments (basically, you are saying what is going inside your function, what it processes) and define output arguments (what is the result, which very likely are those pixels).
  4. after you finished your function, in your first code, you can now call it by using:
output = algo2(input) % call your function, just like any other function
of course, you can have more inputs and outputs.
for you, it could look like this:
function [y] = algo2(img_name) % create function, where output is "y"
% and your input is the name of the picture.
S=imread(img_name); % which gives your code more flexibility. you can change that if you wish.
meven=[0 0 0;
0 1 1;
1 0 1;
1 1 0];
modd=[0 0 1;
0 1 0;
1 0 0;
1 1 1];
no=1;
y =zeros(size(S));
R=zeros(size(S));
l=zeros(size(S));
for i = 1:size(S,1)
for j = 1:size(S,2)
if (S(i, j) == 0)
r=rand([no,2^no-1]);
disp(r)
ro=round(r*(size(meven,1)-1))+1;
disp(ro)
R(i,j)=meven(ro,1);
l(i,j)=meven(ro,2);
else
r=rand([no,2^no-1]);
disp(r)
ro = round(r*(size(modd,1)-1))+1;
disp(ro)
R(i,j)=modd(ro,1);
l(i,j)=modd(ro,2);
end
end
end
y(i,j)=S(i,j);
end % end of the function
now, you can try calling it in your other script as:
y = algo2("text input.png") % will load text input.png
hope I helped.
  3 Kommentare
Stephen23
Stephen23 am 25 Mär. 2023
Note that script has a particular meaning in MATLAB, and it is not synonymous with file:
So the second point should be corrected as:
2. create a new file, which will contain function only. the name of the file should be the same as the name of function, for example, your function will be named "algo2", your file should be named "algo2.m".
Vilém Frynta
Vilém Frynta am 25 Mär. 2023
oh, i see it now.
you're right, i just tried to make it as easy as possible for a new Matlab user to not get lost. because, in the end, you are using "create a new script" button, although it'll be function.
thanks for clarifying though!

Melden Sie sich an, um zu kommentieren.

Kategorien

Mehr zu Encryption / Cryptography finden Sie in Help Center und File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by