Using a custom function in gui

15 views (last 30 days)
Simone B
Simone B on 27 Oct 2014
Commented: Geoff Hayes on 29 Oct 2014
I have a custom function which generates a figure (a plot). I am not able to implement this function in a gui code in such a way this figure appear in a specified axes within the gui window and not in a separate window. Actually, i am able to do this, but i have to paste the function code itself in the gui's, and modify it to force the axes to be the sought ones via its handles axes(handles.axes2); but this way i do not use any link to my custom function.
I was wondering if it is possible to modify my custom function so that when i type its name in the gui, the plot will appear in gui's axes. thanks a lot, Simone

Accepted Answer

Geoff Hayes
Geoff Hayes on 27 Oct 2014
Simone - try passing the axes handle as the last parameter to your function. Perhaps something like the following will do
function myCustomFunction(freq,amp,hAxes)
% hAxes is optional, so check to see if it has been passed in
if ~exist('hAxes','var')
% hAxes was not passed in to this function so need to create a figure
figure;
hAxes = gca;
end
t = linspace(0,1);
x = amp*sin(2*pi*t*freq);
% plot the data on the axes
plot(hAxes,t,x);
In the above simple function, we plot the sine wave given a frequency, freq, and amplitude, amp. The third parameter, hAxes, is the handle to the axes. It is an optional input so the above code handles the case when the last input is provided and when it isn't. We use exist to see if hAxes has been passed into the function, and if not, then we create a figure and set hAxes to the current axes (calling "get current axes", gca).
We then create some data, and plot it on the axes.
For example, calling the below will create a new figure each time
myCustomFunction(4,1)
myCustomFunction(5,2)
but calling
myCustomFunction(10,3,handles.axes2)
will plot the data on the axes given by handles.axes2 (and so will not create a new figure).
  2 Comments
Simone B
Simone B on 28 Oct 2014
Dear Geoff, thanks a lot for your answer. It does work for the simple gui i am working on. It looks like i am able to adapt your suggestion to a more complicated gui i am struggling with. There now the problems might be confined to the creation of some sort of ginput function in which i can specify the handle of the axes i want the crosshair to appear. Indeed, i need to alternatively collect mouse click data confined to each of the axes that have been created. Ordinary ginput, even specifying its relevant axes handle "axes(varargin{i})" before, makes appear a crosshair on all gui's figure and collecting mouse data becomes very inconsistent.
i found this http://www.mathworks.com/matlabcentral/fileexchange/39799-ginput-funtion-to-be-used-in-guis/content/ginputax/ginputax.m but it causes errors: 1. the function "fix" is misplaced and 2. it doesn't understand the "-" minus in the expression "how_many = how_many - 1". I am not able to make it work.
Do you have any idea if this issue can be sorted out somehow? thank a lot again, and sorry to bother. Simone
Geoff Hayes
Geoff Hayes on 29 Oct 2014
Simone - what you are asking should be as a question that is distinct from this one. As for the errors, you should really contact the author of this submission so that he is given a chance to address the issue. However, looking at the code, there seems to be a disconnect between the function signature and the comments on how to use the function. Since the signature is
function [out1,out2,out3,out4] = ginputax(ax,arg1)
then I would pass your handles first followed by the number of points as
ginputax([handles.axes1;handles.axes2],5);
which is just the opposite order of what he describes in his examples. Note that in the above, I'm not putting the handles in a cell array (again as suggested by his code).

Sign in to comment.

More Answers (0)

Categories

Find more on 2-D and 3-D Plots in Help Center and File Exchange

Products

Community Treasure Hunt

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

Start Hunting!