Associating the output of a script with a gui axes

Hi there, I hope someone can help me. I have created a matlab gui with a button that collects a request date from a user and passes the date to a function. The function then produces a plot. How do I get that plot onto my gui? I have an axes object (called axes2). Presently when I press the button on my gui, the plot itself pops up outside the gui as if I am calling it from the command window. Any help would be greatly appreciated, kind regards, mark

 Accepted Answer

It sounds like the function always uses figure() . Unless you can change that (perhaps through optional arguments), the best you can do is:
Wait until the figure has been created, then set the Parent property of the axes in that figure to be the figure of your GUI, then set the Position (and Units) of the axes to be where you want your axes to show up. Those things done, delete the figure that the function popped up, and delete the now-unused axes2 (and possibly saving the new axes handle on top of the variable that stored the axes2 handle.)
If you can change the function, then pass axes2 down to it, and read through my commentary in http://www.mathworks.com/matlabcentral/answers/22208-show-figure

1 Comment

Hi Walter,
Thank you very much. I think I'm still a little unclear. Just started working with gui and get a bit waffled.
I'm attaching the function. (Possibly overkill). The creation of the plot begins at line 189. It has two x axes and two y axis (just to complicate things!)
If you have a suggestion on what to pass in (axes2 or handles.axes2) and do I send something out of the function?
Thanks for your help and patience!
Here's the function:
function[] = gui_VPT_Signal(year, month, day, hour, station)
% The function of this script is to create a series of figures that show
% Virtual Potential Temperature and Windprofilers Signal Strength with
% height up to 7000 meters above the ground. There are three main file
% types used to create these figures: The first file type is the signal data,
% the second file type is the surface data and the third file tyype is the
% rass data otherwise known as the virtual temperature data.
if day < 10
day = strcat(num2str(0),num2str(day));
else
day = num2str(day);
end
if month == 6
monthWord = 'june';
end
if month == 7
monthWord = 'july';
end
if month < 10
month = strcat(num2str(0),num2str(month));
else
month = num2str(month);
end
% create a path variable
pathRa = '/data/sonduk/a/mdempsey/windprofilers/dataDecoded/rass/';
pathSu = '/data/sonduk/a/mdempsey/windprofilers/dataDecoded/surface/';
pathMo = '/data/sonduk/a/mdempsey/windprofilers/dataDecoded/moments/';
fileMo = strcat(pathMo,num2str(year), monthWord,'/',num2str(year),month, day,'o/',num2str(station),'Mo.dat');
fileRa = strcat(pathRa,num2str(year), monthWord,'/',num2str(year),month, day,'o/',num2str(station),'Ra.dat');
fileSu = strcat(pathSu,num2str(year), monthWord,'/',num2str(year),month, day,'o/',num2str(station),'Su.dat');
surface = load(fileSu);
yrSu = surface(:,1);
moSu = surface(:,2);
dySu = surface(:,3);
hrSu = surface(:,4);
mnSu = surface(:,5);
latSu = surface(:,7);
longSu = surface(:,8);
haglSu = surface(:,9);
mslpSu = surface(:,10);
dateSnSu = datenum(yrSu,moSu,dySu,hrSu,mnSu,zeros(size(mnSu)));
rass = load(fileRa);
stidRa = rass(:,1);
yrRa = rass(:,2);
moRa = rass(:,3);
dyRa = rass(:,4);
hrRa = rass(:,5);
mnRa = rass(:,6);
latRa = rass(:,7);
longRa = rass(:,8);
dateSnRa = datenum(yrRa,moRa,dyRa,hrRa,mnRa,zeros(size(mnRa)));
moments = load(fileMo);
stidMo = moments(:,1);
yrMo = moments(:,2);
moMo = moments(:,3);
dyMo = moments(:,4);
hrMo = moments(:,5);
mnMo = moments(:,6);
latMo = moments(:,7);
longMo = moments(:,8);
dateSnMo = datenum(yrMo,moMo,dyMo,hrMo,mnMo,zeros(size(mnMo)));
% now that the files are loaded (surface, rass and moments), any
% data that was missing is replaced by a NaN data value
for rS = 9:32
rass(find(rass(:,rS)==9.990000000000000e+26),rS) = NaN;
end
for mom = 9:32
moments(find(moments(:,mom)==9.990000000000000e+26),mom) = NaN;
end
for sP = 9:13
surface(find(surface(:,sP)==9.990000000000000e+26),sP) = NaN;
end
% the following try statements find the index of the hour parameter and add
% 5 hours for GMT, which is what the data time stamp is based upon.
indexMo = find(hrMo == hour)+5;
indexRa = find(hrRa == hour)+5;
indexSu = find(hrSu == hour)+5;
% set up local time by subtracting 5 hours from
% GMT.
dateTimeRa = datestr(dateSnRa(indexRa) - (5*0.0416))
dateTimeSu = datestr(dateSnSu(indexSu) - (5*0.0416));
dateTimeMo = datestr(dateSnMo(indexMo) - (5*0.0416))
% this is the specific hour which the figure will show
hour = hrRa(indexRa)
% here a matrix is created based on the signal data from the
% moments file
momDat = moments(indexMo,9:32)
% we can build a table of virtual temperatures
virtRa = rass(indexRa,9:32)
% wherever we have NaN data we will note it's index and
% substitute data to interpolate where NaN data was.
isnanIndex = isnan(virtRa)
for m = 1: length(virtRa)
try
if isnanIndex(m) ==1
virtRa(m) = (virtRa(m-1) + virtRa(m-2))/2 + ((virtRa(m-1) - virtRa(m-2) ))
end
catch err
continue
end
end
tvsurf = virtRa(1)
zsurf = haglSu(1)
% These are constants for gravity and for Real gas.
grav=9.8;
rgas=461.7;
% this represents the exponent in the hypsometric equation
exp1=(-grav/(rgas*tvsurf))*zsurf;
exp2 =(-grav/(rgas*tvsurf))*500;
% this represents the calculation of the surface pressure by using the
% hypsometric equation
% setting the surface ot 1000 is misleading
% as this does not take into account the
% actual Z height. Instead, substitute 1000
% for mslpSu(n) (no data) since exp1
% contains the zsurf variable
if mslpSu(indexSu) == 9.990000000000000e+26
psurf = 1000*exp(exp1)
else
psurf=mslpSu(indexSu)*exp(exp1);
end
%psurf=mslpSu(n)*exp(exp1);
% this represents the exponent of the hypsometric equation with dZ being
% used 250 m. It calculates a Pressure fro every level listed in tv
exponent=(-grav/rgas)*250./virtRa;
% This creates and array of 1s which I will use to populate with my
% pressures
press=ones(1,24);
% this represents the calculation of my surface pressure, the answer with
% which we use to populate the first index of press
press(1) = psurf.*exp(exp2);
% this is a for loop used to calculate each individual's pressure and
% populate index 2 through 200 of press
for lev = 2:24
press(lev)=press(lev-1).*exp(exponent(lev));
end
% kappa is a constant
kappa=0.286;
% this represents the exponent of the formula below it (I can clarify here)
factor=(1000./press).^kappa;
% this calculates thetav from virtual temperature
thetav=virtRa.*factor;
y = 500:250:6250;
% the following are the axis labels
xlabels{1} = 'Theta v (K)';
xlabels{2} = 'Signal strength (MHz)';
ylabels{1} = 'Height above ground (m)';
ylabels{2} = 'Height above ground (m)';
% the first plot is the virtual temperature and that is
% commented out. The second plot is the theta V and that is
% black, the third is the signal data. It is only plotted here
% so that the legend will contain it.
%plot(virtRa,y,'b')
%hold on
plot(thetav,y,'k')
hold on
plot(momDat, y, 'r')
ax1 =gca
set(ax1, 'XColor','k','YColor', 'k','XLim',[285,315], 'YLim',[0,7000],'Position',[0.12 0.12 0.75 0.70])
% the fourth plot is, again, the signal data, plotted on the
% upper x axis and the right hand side y axis
ax2=axes('Position',get(ax1,'Position'),...
'XAxisLocation','top',...
'YAxisLocation','right',...
'Color','none',...
'XColor','r','YColor','r','XLim',[50,80], 'YLim',[0,7000]);
hl2 = line(momDat,y,'Color','r','Parent',ax2)
legend(ax1,'Theta V', 'Signal Return')
hold off
title(strcat('Station 74',num2str(station),' local data and time: ', dateTimeRa), 'FontWeight', 'Bold')
%label the two x-axes
set(get(ax1,'xlabel'),'string',xlabels{1})
set(get(ax2,'xlabel'),'string',xlabels{2})
set(get(ax1,'ylabel'),'string',ylabels{1})
set(get(ax2,'ylabel'),'string',ylabels{2})
end
here's the button callback:
% --- Executes on button press in pushProfileVPT.
function pushProfileVPT_Callback(hObject, eventdata, handles)
% hObject handle to pushProfileVPT (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
yeare = str2num(get(handles.editYearVPT,'String'));
monthe = str2num(get(handles.editMonthVPT, 'String'));
daye = str2num(get(handles.editDayVPT, 'String'));
houre = str2num(get(handles.editHourVPT, 'String'));
statione = str2num(get(handles.editStationVPT, 'String'));
gui_VPT_Signal(yeare, monthe, daye, houre, statione);

Sign in to comment.

More Answers (0)

Categories

Find more on Graphics Object Properties 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!