How to save persistent variables after the last call to the function?

2 ビュー (過去 30 日間)
MAHABUB HASAN MAHALAT
MAHABUB HASAN MAHALAT 2017 年 2 月 27 日
編集済み: MAHABUB HASAN MAHALAT 2017 年 2 月 28 日
I have a function with some persistent variables, when I am writing the persistent variables (using save command) during each call to the function than it is working fine but when I am using if else logic to write the variables only at the last call to the function then Matlab storing the variables as empty. I have attached the required code segment.
function [y]=fitness5(x)
persistent stor_chrom;
persistent store_fitness;
persistent store_counter;
persistent Population1;
flag=0;
if (isempty(Population1))
load('C:\Users\PC01\Documents\MATLAB\Population2.mat');
Population1 =Population2;
end
if (isempty(store_counter))
store_counter = 0;
end
if (isempty(stor_chrom))
stor_chrom=[];
end
if (isempty(store_fitness))
store_fitness = [];
end
if x==-1
save stor_chrom1.mat stor_chrom store_fitness;
else
%some code......
% update persistent variables
store_counter=store_counter+1;
stor_chrom(store_counter,:)=x;
store_fitness(store_counter,1)=chor_no;
store_fitness(store_counter,2)=fit2;
store_fitness(store_counter,3)=final_gates;
y=fit2;
end
  1 件のコメント
Jan
Jan 2017 年 2 月 27 日
編集済み: Jan 2017 年 2 月 27 日
Then your code contains a bug. Please post the (relevant part of the) code by editing the original question (not as comment or answer). Then we can examine, what's going on, e.g. what exactly "using if else logic" means.

サインインしてコメントする。

回答 (1 件)

Alexandra Harkai
Alexandra Harkai 2017 年 2 月 27 日
Without using the save command the persistent variables could be declared as persistent when calling from a function, as explained in the documentation .

カテゴリ

Help Center および File ExchangeArgument Definitions についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by