How do I execute this script as a function?
조회 수: 2 (최근 30일)
이전 댓글 표시
My group and I are currently adding to an open source piece called IR-view. We have added a push button to its control panel and we are trying to put some functionality to it. The current control panel allows us to extract frames from an uploaded RAW file in the form of a 3D matrix. We have written a short script that extracts the highest numerical value from each frame and plots each of these high points on a graph. The script by itself works fine. My question is how do I execute this script from the push button? I have tried using callback and I tried making the script using a function but I am getting an error that says "To many input arguments". This is what we have:
for i=1:frames
frame = allf(:,:,i);
largest(i) = max(frame(:));
[num idx]= max(frame(:));
[row column]= ind2sub (size(frame),idx);
locationx(i)=row;
locationy(i)=column;
end
figure (1)
scatter(locationy,locationx)
set(gca,'YDir','reverse')
Where 'allf' is what we rename the matrix when we extract it. frames refers to the amount of frames in the RAW file (in this case its 50). The script as you see it works fine. But it needs to run from a push button.
Many thanks
댓글 수: 0
답변 (2개)
Adam
2015년 3월 11일
편집: Adam
2015년 3월 11일
set( hPushButton, 'Callback', @(src,evt) func( allf ) )
where you create the pushbutton (obviously you can just add it to the construction of the uicontrol too). Then create the following somewhere on your path:
function func( allf )
for i=1:frames
frame = allf(:,:,i);
largest(i) = max(frame(:));
[num idx]= max(frame(:));
[row column]= ind2sub (size(frame),idx);
locationx(i)=row;
locationy(i)=column;
end
figure (1)
scatter(locationy,locationx)
set(gca,'YDir','reverse')
should do the trick. Obviously use a sensible function name though!
댓글 수: 4
Adam
2015년 3월 11일
I can't see any obvious reason why that would be the case. Have you tried leaving it where it was when it was a script and just adding the function line at the top of the file to turn it into a function there?
Image Analyst
2015년 3월 11일
If you're using GUIDE, just call the function from within the button's callback function. Let's say your function is called FindMax() and it takes allf and frames as input arguments. Then call it in the callback function like this
[x, y] = FindMax(allf, frames);
Then define it like
function [locationx, locationy] = FindMax(allf, frames)
% The code goes here.
By the way, is there any reason why you're defining locationx as the row instead of the column and locationy as the column instead of the row? Usually x is the horizontal distance and y is the vertical distance and you're swapping and going against the convention for some reason.
댓글 수: 2
Image Analyst
2015년 3월 11일
It's not getting inverted. It's getting transposed. There is a BIG difference. But since you're doing scatter(locationy,locationx) and swapping x and y you're not noticing the difference.
참고 항목
카테고리
Help Center 및 File Exchange에서 Interactive Control and Callbacks에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!