How do I execute this script as a function?
1 view (last 30 days)
Show older comments
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 Comments
Answers (2)
Adam
on 11 Mar 2015
Edited: Adam
on 11 Mar 2015
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 Comments
Adam
on 11 Mar 2015
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
on 11 Mar 2015
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 Comments
Image Analyst
on 11 Mar 2015
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.
See Also
Categories
Find more on Migrate GUIDE Apps in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!