MATLAB: How to show msbackadj plot within app window.

app designerbaseline correctionfiguremsbackadjplot

Hello Everyone,
Recently I have been attempting to make a graphic user interface (GUI) for some of the scripts previously written by one of the grad students in our lab to make it easier for students who don't have experience with editing code to use. One of the scripts is for correcting the baseline of our Raman data, for this he used the built-in msbackadj function. Now this works fine as is but to keep everything neat and tidy I've been trying to keep everything within the main app window, I have two graphs in the app to handle any of our plots, and I'm not sure how to put the plot of msbackadj into one of these windows instead of spawning its own figure window.
I've included my code for the Baseline correction button but the only really important part there is the
ICorr = msbackadj(app.Pixel2,app.I2,'WindowSize',app.window,'StepSize',app.step,'Showplot',true);
line. Setting Showplot to true pulls up a separate figure window showing the plot of Pixel2 and I2 alongside what is being subtracted from that plot. I'd like to put all of this into the UIAxes that I have on the main app window.
Any assistance or advice anyone has would be greatly appreciated. Thanks in advance.
% Button pushed function: BaselineCorrectionTestButton
function BaselineCorrectionTestButtonPushed(app, event)
%Adjust the "Window Size" and "StepSize" until you achieve a baseline
%correction which looks good. Use these values in "BaselineCorrAll"
%Note: Baseline correction must be done BEFORE calibration
ICorr = msbackadj(app.Pixel2,app.I2,'WindowSize',app.window,'StepSize',app.step,'Showplot',true);
%Plot the baseline corrected data next to the original.
% figure;
plot(app.UIAxes2, app.Pixel2, app.I2);
hold (app.UIAxes2, 'on')
plot (app.UIAxes2, app.Pixel2, ICorr);
hold (app.UIAxes2, 'off')
end

Best Answer

The msbackadj() function produces a figure when ShowPlot is true or equal to any integer value that specifies a column of input data. There's no way to specify an axes so it will either produce a new figure or it won't produce a plot at all.
I see two ways around this.
The easy way
Use copyobj() to copy the content of the msbackadj() plot on to your app axes and then destroy the external figure.
Pro: It's easy to implement and only requires a few lines of code.
Con: You may see a figure appear and quickly disappear which can be annoying.
See inline comments for details
function BaselineCorrectionTestButtonPushed(app, event)
% Clear your axes
cla(app.UIAxes2)
% Produce your data and external figure
ICorr = msbackadj(app.Pixel2,app.I2,'WindowSize',app.window,'StepSize',app.step,'Showplot',true);
% get handle to the axes that were just created in the new figure
newAx = gca();
% Copy all children of the new axes to your UIAxes
% This will not copy the legend but you could get the handle to the
% legend and apply copyobj() to the legend-handle if you want to copy that, too.
% This will also not copy the xlabel and ylabel but that should be easy to
% reproduce.
copyobj(newAx.Children, app.UIAxes2)
% delete the external figure
delete(newAx.Parent)
end
The hard way
Open the msbackadj() function and search for the word "figure" (there is only 1 match in the r2019b file). Set a break point at that line and then run your code so that it stops prior to producing the figure. Then step though each line to determine what is being plotted and how those variables were calculated. You can then reproduce those lines of code in your app.
Pro: No external figure needed and it may run a tad faster than the other method (if noticeable at all).
Con: It will take more time to get it to work since there's a lot going on in that function that varies depending on the inputs.