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);

 채택된 답변

Star Strider
Star Strider 2020년 9월 22일

0 개 추천

See the documentation on Function Basics.

댓글 수: 9

marie lasz
marie lasz 2020년 9월 22일
thanks but I am confused and would like to learn from the code given above
Star Strider
Star Strider 2020년 9월 22일
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 2020년 9월 22일
편집: marie lasz 2020년 9월 22일
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
Star Strider
Star Strider 2020년 9월 22일
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.
marie lasz
marie lasz 2020년 9월 22일
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.
Steven Lord
Steven Lord 2020년 9월 22일
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.
marie lasz
marie lasz 2020년 9월 22일
thanks for your suggestions and finally, I did it successfully and learned :-)
Star Strider
Star Strider 2020년 9월 22일
Our pleasure!
If my Answer helped you solve your problem, please Accept it!
.

댓글을 달려면 로그인하십시오.

추가 답변 (1개)

hatem
hatem 2023년 12월 27일

0 개 추천

>> 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);

카테고리

도움말 센터File Exchange에서 Loops and Conditional Statements에 대해 자세히 알아보기

질문:

2020년 9월 22일

답변:

2023년 12월 27일

Community Treasure Hunt

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

Start Hunting!

Translated by