my code is getting skipped

my m file runs properly halfway but it skips(does not display output image)for some codes
it does not show any error.this is my code it does not run altogether.if i remove 3rd part 2nd executes,or else 2nd does not.1 does sometimes i want all of them to run(this is half code of my m-file)
1st:
g=rgb2gray(Image)
figure,surf(double(g),'edgecolor','none')
%axis([0 columns 0 rows min(double(g(:))) max(double(g(:)))])
colormap hot
2: im=(Image)
mean(im(:))
if mean(im(:))>=10 & mean(im(:))<=47
bargred0
end
if mean(im(:))>=47 & mean(im(:))<=50
bargred0
end
3: a=mean(im(:))
r=.5
t=1:1:10
x1=100*(1+r).^t
x2=a*(1+r).^t
plot(x1)
hold on
plot(x2,'--')
xlabel('Days')
ylabel('Growth Rate')
legend('General','Acquired')

4 Comments

I would suggest that you step through using the debugger.
Indeed...as written, 3: will always execute whereas there'll be nothing shown in 2: if the average is outside the range [10 50].
As a comment, I'd compute and save the mean early on in the code to not have to recompute it a half-dozen times or more...
More than likely if there is a coding problem it's either--
a) the data isn't what you think and the condition isn't satisfied, or
b) probably even more likely, the problem is in something you haven't shown.
Agree w/ the other respondent -- use the debugger thru the entire code for a case that you think is troublesome.
ok,i set a breakpoint , and using steps went through the code, everything worked but,when a first figure displays it gets replaced by next figure,thereby showing the first and last figures only.

Sign in to comment.

Answers (2)

Image Analyst
Image Analyst on 7 Apr 2014
Edited: Image Analyst on 7 Apr 2014
After you view this http://blogs.mathworks.com/videos/2012/07/03/debugging-in-matlab/ and use what you learn, you will know why it's skipping past some lines of code. By the way, you need to change these lines to:
meanGL = mean2(im);
if meanGL >= 10 && meanGL <= 50
bargred0
end
Use mean2() instead of mean(), call it only once, use && instead of &, and combine two ifs into one.

17 Comments

i did as you said now actually only 3rd part executes. if i somehow delete something from 2 part it shows error and displays wanted figures. also could having too many if statements be a problem in this
Attach your m-file and image (or else change your code to use a standard demo image).
Image Analyst
Image Analyst on 7 Apr 2014
Edited: Image Analyst on 7 Apr 2014
The code you attached doesn't run. For one thing, the bargraphnn functions are not included. Plus you say "sometimes i want all of them to run" - well, when is that? As you have it now, only one if block runs depending on what the mean is.
i hope these many should do, i wonder if so many if statements are causing it
Answer this:
you say "sometimes i want all of them to run" - well, when is that? As you have it now, only one if block runs depending on what the mean is.
i want all of them to run, but sometimes only two of them run and one doesnt. i dont know whats wrong
Well, get rid of all the "if" statements! Just call bargraph0, etc. and don't check what the mean is - just run it regardless of what the mean is.
nida
nida on 7 Apr 2014
Edited: nida on 7 Apr 2014
i am using those ranges cause ill be using different images and with different ranges shows diff bars.i could scale 'if ' down.but i really need it to work. isnt there a solution. it is working separately(m-file with 'if' statements)
When do you want to call bargraph0? All the time or only when (meangl>=30 && meangl<=40) ???
You can't call it all the time and also only when the mean is in a certain range . That does not make sense.
nida
nida on 7 Apr 2014
Edited: nida on 7 Apr 2014
i have different bargraphs(1,2,3) for different ranges.
sorry for troubling you,but i need this to work
What's that image about? If you're wanting to measure color or something between different images you better be sure you have an color calibrated system. I don't see any color chart in there.
its just data about image if mean falls within range certain bar graph displays thats it
If you change the exposure, or use a flash, on your camera then you could get different brightnesses even though the dirt did not change one bit.
i dont understand
If you want to classify what kind of dirt it is and you say that a gray level of 80 means it's black loam, then what happens when you come tomorrow when it's cloudy and the gray level is only 50? Or you come again next month and it's bright and sunny and the gray level is 150? Are you going to call it beach sand instead of black loam because it's brighter? It's the same dirt - you should not classify it as something different. It just changed because of the exposure or lighting, but it's the same dirt.
ok,but what about my problem

Sign in to comment.

if mean(im(:))>=10 & mean(im(:))<=47 :- Second 'if'
if mean(im(:))>=47 & mean(im(:))<=50 :- Third 'if'
I think,you are getting problem when mean = 47. This is because when mean = 47,second 'if' will be executed and third will be ignored.You can try this -
if mean(im(:))>=10 & mean(im(:))<=47 :- Second 'if'
if mean(im(:))>47 & mean(im(:))<=50 :- Third 'if'

Categories

Find more on Historical Contests in Help Center and File Exchange

Asked:

on 7 Apr 2014

Commented:

on 8 Apr 2014

Community Treasure Hunt

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

Start Hunting!