How do I execute this script as a function?

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

Answers (2)

Adam
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

Thank you for your help, this script is separate from the main source code. Because its a small function we copied and paste the code into the main body but now its highlighting every use of the word 'function' as a syntax error.
Do functions in your main source code terminate with
end
If they do then this one must also. If they don't then this one must not either.
thanks, yes they all finish with 'end' and I changed this so that it does as well. I even retyped the code by hand but still get the same problem.
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?

Sign in to comment.

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

thanks, I will try this. Yes the axis get inverted in the image, in Ir view the image is upside down if the axis is not inverted.
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.

Sign in to comment.

Categories

Find more on Interactive Control and Callbacks in Help Center and File Exchange

Tags

Asked:

on 11 Mar 2015

Commented:

on 11 Mar 2015

Community Treasure Hunt

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

Start Hunting!