0

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

ECrespo
  • 135
  • 1
  • 1
  • 8
  • use `drawnow` to force MATLAB to execute the plot right away – max May 25 '20 at 05:28
  • Thanks, but that only solves the problem of waiting to the end to plot. The main question is related with deleting old model results while keeping the experimental ones before plotting the model results for the new function evaluation. – ECrespo May 25 '20 at 08:32
  • just plot the experiment data and the new simulation data and call `drawnow`. I don't see a problem there – max May 25 '20 at 10:54
  • I just wanted a solution in which I did not want to plot the experimental data every iteration.. but just plot it once in the main file and keep it throughout the process. I know I can solve the issue by defining plotRHO plotKAPPA and plot ALFA as global variables. I just dont understand why and I am sure there is a way to do it without using global variables. – ECrespo May 26 '20 at 15:57
  • than use `hold on` – max May 27 '20 at 05:19
  • The hold on is already in the code I posted. The issue is the deleting of the previous iteration model results before plotting the new ones. – ECrespo May 29 '20 at 17:29

1 Answers1

0

I actually solved my problem by setting the plotRHO , plotKAPPa and plotALFA as global variables in the file where I have my objective function. I am sure there should be a more clever way to do it and avoid the use of global variables but I did not manage to get it.

ECrespo
  • 135
  • 1
  • 1
  • 8