Do the code N times without copying

1 view (last 30 days)
Hello , I want to run my code then get a result from it then do the code again with the first RESULT only as input. Ps i dont want to copy all the code because it's so long .
example :
enter input1,
THE CODE
RESULT
.......
for i=RESULT-0.5:0.1:RESULT+0.5
THE CODE ;
end
thanks for attention .

Accepted Answer

Image Analyst
Image Analyst on 28 Apr 2012
Place your code in a function. This can either be in the same file as your main function, or in a different file. Look up functions in the help and check out Doug Hull's videos on MATLAB Central to learn how to use functions. It's one of the most basic things you need to learn, and most people are using them easily after their first few hours of using MATLAB.
  3 Comments
Image Analyst
Image Analyst on 29 Apr 2012
No, that's not the problem. My programs are typically 3000 - 15000 lines long and have lots of complicated things going on. The problem is you're still learning and just need to try some things. Your Doug function won't work because it's probably just an example or some pseudocode that you were supposed to modify, not take it literally. For example, in an m-file called test1.m you could have this:
function test1() % function name is the same as the m-file name!!
% Now call Doug and put the result into outMain.
outMain = Doug(1,3,5,7,2);
% Now define Doug in the same m-file:
function out = Doug(r, L, N, RN, RR)
var = r + L*N - RN/RR;
rejectionratio = 42 + 69 * exp(pi/123);
out = rejectionratio+(var)
Again, this was just an example and you need to actually put your own code in there, not just take what I did. Even though the code I gave works, it's just gibberish.
Ahmed Hassaan
Ahmed Hassaan on 30 Apr 2012
It wasnt working becuz of clc and clear all in the beginning of the code thanks for ur help :)

Sign in to comment.

More Answers (0)

Categories

Find more on Get Started with MATLAB in Help Center and File Exchange

Products

Community Treasure Hunt

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

Start Hunting!