Matlab Function from code

Hello Experts;
I am new in matlab and trying to learn how we can create function? For Example . I have this code how I can create function file out of it? It will be easy for me to learn from this code.
g=imread('cameraman.tiff');
original=g;
[m,n]=size(g);
subplot(2,2,1)
imshow(g,[])
[m,n]=size(g);
a = 3;
b = 3;
num_iter = 5;
for k=1:num_iter
for i=1:m
for j=1:n
r = mod([round(abs(1-(a*(i^4))+j)),round(abs(b*i))],[m n]);
x(i,j)=g(r(3)+1,r(4)+1);
end
end
g=x;
end
subplot(2,2,2)
imshow(x);

 Accepted Answer

Star Strider
Star Strider on 22 Sep 2020
See the documentation on Function Basics.

9 Comments

thanks but I am confused and would like to learn from the code given above
Experiment. The world will not end if your code throws errors. MATLAB is reasonably robust.
Figure out what arguments (inputs) the function needs, and what outputs it should return to the workspace that called it. Then, write the function to do that.
Note that the function does not necessarily need arguments or outputs, since how you write it depends on what you want it to do.
marie lasz
marie lasz on 22 Sep 2020
Edited: marie lasz on 22 Sep 2020
what do you mean by your indirect phrases 'The world will not end if your code throws errors.'?? And I didn't ask you about code. If you don't want to answer then don't throw these phrases , no one is forcing you to answer. Thanks
I mean that I am encouraging you to experiment! The worst possible outcome is that your code throws an error. That is frustrating (and occasionally embarrassing), however when I experiment, I always learn from my mistakes, and generally do not repeat them afterwards.
There is an extremely remote possibility that your code could crash MATLAB or crash your computer, however that is unlikely. MATLAB has become much more robust over the years, and catches most such programming errors either when they are written (if you use the MATLAB Editor), or when the code executes.
Yes I tries and trying to learn. I never relies on help but I appreciate if someone tries to teach me. Anyways thanks .
That looks to be a good start.
Save it as: shuf.m somewhere on your MATLAB user path (if you have not already done so), then call it from a script to see if you get the result you expect.
Remember to call it as:
x = shuf(g,num_iter);
with the appropriate agrguments. The ‘x’ output will then be in your workspace.
In addition to what Star Strider has said, if you're not sure if your code is behaving correctly I recommend stepping through it line by line, checking that each line does what you expect it to do. The debugging tools in MATLAB will let you do this. Set a breakpoint on the first line, run your code, and step through the file.
thanks for your suggestions and finally, I did it successfully and learned :-)
Our pleasure!
If my Answer helped you solve your problem, please Accept it!
.

Sign in to comment.

More Answers (1)

hatem
hatem on 27 Dec 2023
>> x =imread('cameraman.tif');
>> y = x*0;
>> [w h]= size(x);
>> for i = 1:w
for j =1:h
b =bitget(x(i,j),6)
y(i,j)=bitset(y(i,j),6,b);

Categories

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

Asked:

on 22 Sep 2020

Answered:

on 27 Dec 2023

Community Treasure Hunt

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

Start Hunting!