Adding index to a function.

2 ビュー (過去 30 日間)
Kevin P Meyer
Kevin P Meyer 2021 年 10 月 7 日
コメント済み: Matt J 2021 年 10 月 8 日
Hello, I am trying to add an index to a function. I have an ROI on an image. Each time the ROI moves on the image, I want the index to increase by 1. So when I move the ROI for a fourth time i want the index to be equal to 4. I have the index sum = 0, and then when the ROI moves I say sum = sum+1;. However, when I do this, the function repeats and just sets the sum = 0, and so the sum is always equal to 1, instead of increasing by integer values. Does anyone have an idea on how to fix this. I tried putting the sum = 0 line on the outside of the function but then the function doesn't know what sum is and it is undefined.
clear
clc
I = imread('air.jpg');
I = rgb2gray(I);
I = im2double(I);
imshow(I)
roi = drawrectangle('Color','r');
addlistener(roi,'MovingROI',@allevents)
addlistener(roi,'ROIMoved',@allevents)
function allevents(src,evt)
evname = evt.EventName;
I = imread('air.jpg');
I = rgb2gray(I);
I = im2double(I);
sum = 0;
switch(evname)
case{'MovingROI'}
%donothing for now
case{'ROIMoved'}
sum = sum+1
disp(['ROI moved previous position: ' mat2str(evt.PreviousPosition)]);
disp(['ROI moved current position: ' mat2str(evt.CurrentPosition)]);
end
end

採用された回答

Matt J
Matt J 2021 年 10 月 7 日
編集済み: Matt J 2021 年 10 月 7 日
One way is to nest the function inside an outerFunction() and make the accumulator an externally scoped variable. In the example below, I've renamed your accumulator from sum to accum since the latter avoids conflict with the builtin Matlab sum() function.
function outerFunction
accum=0; %<-----------MOVE HERE
I = imread('air.jpg');
I = rgb2gray(I);
I = im2double(I);
imshow(I)
roi = drawrectangle('Color','r');
addlistener(roi,'MovingROI',@allevents)
addlistener(roi,'ROIMoved',@allevents)
function allevents(src,evt)
evname = evt.EventName;
I = imread('air.jpg');
I = rgb2gray(I);
I = im2double(I);
switch(evname)
case{'MovingROI'}
%donothing for now
case{'ROIMoved'}
accum = accum+1
disp(['ROI moved previous position: ' mat2str(evt.PreviousPosition)]);
disp(['ROI moved current position: ' mat2str(evt.CurrentPosition)]);
end
end
end
  1 件のコメント
Kevin P Meyer
Kevin P Meyer 2021 年 10 月 7 日
Thank you so much! I have been stuck on that for a while.Makes sense

サインインしてコメントする。

その他の回答 (1 件)

Matt J
Matt J 2021 年 10 月 7 日
編集済み: Matt J 2021 年 10 月 7 日
You could also have used a persistent variable.
function allevents(src,evt)
persistent accum
if isempty(accum), accum=0; end %first time called
evname = evt.EventName;
I = imread('air.jpg');
I = rgb2gray(I);
I = im2double(I);
switch(evname)
case{'MovingROI'}
%donothing for now
case{'ROIMoved'}
accum = accum+1
disp(['ROI moved previous position: ' mat2str(evt.PreviousPosition)]);
disp(['ROI moved current position: ' mat2str(evt.CurrentPosition)]);
end
end
  2 件のコメント
Kevin P Meyer
Kevin P Meyer 2021 年 10 月 7 日
Ah, okay. The nested function works well for my applications right now but I may try that way to see how it affects the speed. Do you think I could make my image, I, a persistent variable? Would this mean that the function wouldn't have to read it each time?
Matt J
Matt J 2021 年 10 月 8 日
Yes, you can make any variable a persistent variable.

サインインしてコメントする。

カテゴリ

Help Center および File ExchangeImages についてさらに検索

製品

Community Treasure Hunt

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

Start Hunting!

Translated by