I am facing an issue related with the need to update part of a plot at every iteration of fminsearch.
In summary, in my "main_optimization" file I have this part of a code that is intended to plot the experimental data to be fitted by my model. This are the plots I want to keep during the whole process:
figure (1)
subplot(1,3,1)
plot(Pexp,Rexp(:,1),'ro',Pexp,Rexp(:,2),'go',Pexp,Rexp(:,3),'bo');
xlabel('P/MPa');
ylabel('\rho /mol.L^{-1}');
subplot(1,3,2)
plot(Pexp,Aexp(:,1),'ro',Pexp,Aexp(:,2),'go',Pexp,Aexp(:,3),'bo');
xlabel('P/MPa');
ylabel('\alpha /K^{-1}');
subplot(1,3,3)
plot(Pexp,Kexp(:,1),'ro',Pexp,Kexp(:,2),'go',Pexp,Kexp(:,3),'bo');
xlabel('P/MPa');
ylabel('k_{T} /K^{-1}');
Then I call fminsearch to do the optimization of my model and inside my objective function file I do the following with the objective of deleting the model results from previous iteration and plotting the new ones.
if ishandle(plotRHO)==1
delete(plotRHO);
end
if ishandle(plotALFA)==1
delete(plotALFA);
end
if ishandle(plotKAPPA)==1
delete(plotKAPPA);
end
subplot (1,3,1)
hold on
plotRHO=plot(Pexp,rhocalc(:,1),'r-',Pexp,rhocalc(:,2),'g-',Pexp,rhocalc(:,3),'b-');
legend(num2str(AAD_RHO),'Location','southeast');
hold off
subplot (1,3,2)
hold on
plotALFA=plot(Pexp,apcalc(:,1),'r-',Pexp,apcalc(:,2),'g-',Pexp,apcalc(:,3),'b-');
legend(num2str(AAD_AP),'Location','southeast');
hold off
subplot (1,3,3)
hold on
plotKAPPA=plot(Pexp,ktcalc(:,1),'r-',Pexp,ktcalc(:,2),'g-',Pexp,ktcalc(:,3),'b-');
legend(num2str(AAD_KT),'Location','southeast');
hold off
I tried different stuff like initializing the plotRHO, plotALFA, plotKAPPA in the main file with either an initial plot or {} and then give them as input to the objective function so that I could avoid getting an error in the first iteration in the ishandle function. When I do this it deletes the first plot and draws the line of the first iteration but then it never plots again nor deletes the old one because the handle is always 0 (which I believe it should not).
If I remove the ishandle and just delete them because I know they exist, they dont delete anything and an warning is displayed saying the object could not be found.
What is the smartest way to always keep my experimental points and just update the model results at each function evaluation of my objective function? It either keeps plotting every line (and just opens the figure when I stop the program due to (memory issues?) or is only plots the first time and then never updates nor deletes despite the program is optimizing..
Thank you in advance for any help,
Best regards
PS: Sorry for the long post
Update: I don't really understand why but if I define plotRHO, plotALFA and plotKAPPA as globals in the beginning of my objective function file, the delete and replot works as I want to. But as far as I understand global variables should be avoided and surely they can be in this case as well