How to save a data when something happens

Hello everybody, I am receiving 100 symbols, with values starting from 0 and go to 1. The scenario is like this.
for i=1:100
if Symbolvalue >= 0.5
Nuberofsymbolvalue=i;
end;
end;
I need to save the the first i in which I go above 0.5
Thanks in advance.

답변 (3개)

Image Analyst
Image Analyst 2013년 11월 17일
편집: Image Analyst 2013년 11월 17일

0 개 추천

if symbolValue > 0.51
savedSymbolValue = symbolValue;
% Optionally save to a mat file
save(yourMatFileName, 'savedSymbolValue');
end

댓글 수: 5

thanx a lot. But i dont want to save symbol value, I want to save the number in which symbol value goes above 0.5 if I use a if like this:
if symbolvalues >= 0.5
Numberofsymbol=n;
end;
this way, I am not able to save the first time I go above my threshold!
I want to save the number of that symbol in which the first time go above the threshold.
The scenario is like this, But I need to save the the first i in which I go above 0.5
for i=1:100
if Symbolvalue >= 0.5
Nuberofsymbolvalue=i;
end;
end;
OK, you didn't have any explanation at first so I just went with what I could. Now that I see what you're after I recommend:
Nuberofsymbolvalue = find(Symbolvalue > 0.5, 1, 'first'); % Save the index
No loop over i is needed. If you wanted to find out the value at that index, do this:
theSymbolValue = Symbolvalue(Nuberofsymbolvalue);
though you might want to use Number instead of Nuber (unless Nuber means Number in your language)
Thanks a lot for your answer. the find() function solution is way simpler and i am gonna try to use it in my codes. Thanks a agin.
Image Analyst
Image Analyst 2013년 11월 18일
Please mark the best answer as "Accepted" if you're done with this topic. Thanks.

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

Umair Nadeem
Umair Nadeem 2013년 11월 17일
편집: Umair Nadeem 2013년 11월 17일

0 개 추천

you can use this
index = 1;
for i = 1:100
if SymbolValue >= 0.5
NumberofSymbolValue[index] = i;
index += 1;
end
end
Valueoffirst_i = NumberofSymbolValue[1];
The first value in array NumberofSymbolValue will show the first i
Hope it helps

댓글 수: 4

MATLAB does not use { and } for grouping: it uses "end" to mark the end of a block.
Thanks a lot Umair, your solution worked :))))
Image Analyst
Image Analyst 2013년 11월 17일
편집: Image Analyst 2013년 11월 17일
How did it work? Are you programming in C? MATLAB doesn't use [] around array indexes and doesn't have the += operator. Plus, his solution could be done with a non-looping simple call to find like Walter and my solution.
allHighSymbolIndexes = find(Symbolvalue >= 0.5)
of course I changed the C to Matlab code. the "find()" function solution is way simpler, I am gonna try to use it in my codes. But Umair's solution worked for may case. Thanks anyway.

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

Walter Roberson
Walter Roberson 2013년 11월 17일

0 개 추천

find(Symbolvalue >= 0.5, 1, 'first')

댓글 수: 2

Thanks a lot Walter, your solution looks really tempting but I dont know why it didnt work for my case! anyway, thanks a lot.
Just assign the output to something:
firstHighSymbolIndex = find(Symbolvalue >= 0.5, 1, 'first')
theFirstHighSymbol = Symbolvalue(firstHighSymbolIndex);
We don't know why it didn't work because you didn't show your code. The code you used (a for loop) is not vectorized and not as MATLAB-ish but is probably still fast enough.

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

카테고리

도움말 센터File Exchange에서 Startup and Shutdown에 대해 자세히 알아보기

질문:

2013년 11월 17일

댓글:

2013년 11월 18일

Community Treasure Hunt

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

Start Hunting!

Translated by