Effacer les filtres
Effacer les filtres

Invalid use of operator

5 vues (au cours des 30 derniers jours)
Ikhsan Romli
Ikhsan Romli le 9 Fév 2023
Commenté : Ikhsan Romli le 15 Fév 2023
Please help me!. I have a problem with this code. When I execute this program. The result is invalid use of operator.
WorkName='Battery'; % work results are saved by WorkName
TimeUnit='weeks'; % time unit name
dt=5; % time interval (five weeks)
measuData=[1.0000 0.9351 0.8512 0.9028 0.7754 0.7114 0.6830 0.6147 ...
0.5628 0.7090]'; % measured data at every time intervals (k1 x 1)
thres=0.3; % threshold - critical value
ParamName=['x'; 'b'; 's']; % model parameters' name to be estimated
initDisPar=[0.9 1.1; 0 0.05; 0.01 0.1];
% probability parameters of initial distribution, pxq
% (p: num. of unknown param, q: num. of probability param)
n=5e3; % number of particles
signiLevel=5; % significance level for C.I and P.I
%==========================================================================
% % % PROGNOSTICS using PARTICLE FILTER
p=size (ParamName,1);
for j=1:p; % Initial Distribution
param(j,:)=unifrnd(initDisPar(j,1),initDisPar(j,2),1,n);
ParamResul(j,:)=[ParamName(j,:) 'Resul'];
eval([ParamResul(j,:) '=param(j,:);']);
end;
kl=length(measuData); k=1; % Update Process or Prognosis
if measuData (end)-measuData(1)<0; cofec=-1; else cofec =1; end
while min(eval([ParamResul(1,:) '(k,:)'])*cofec)<thres*cofec; k=k+1;
% stepl. predlotion (prior)
paramPredi=param;
for j=1:p; eval([ParamName(j,:) '=paramPredi(j.:);']); end
paramPredi(1,:)=...%====== PROBLEM DEFINITION: MODEL DEFINITION=============
exp(-b.*dt).*x;
%==========================================================================
if k<=k1 % (Update Process)
% step2. update (likelihood)
like1=normpdf(measuData(k),paramPredi(1,:),paramPredi(end,:));
% step3. resampling
cdf=cumsum(like1)./max(sum(likel));
for i=1:n;
u=rand;
loca=find(cdf >= u); param(:,i)=paramPredi(:,loca(1));
end;
else % (Prognosis)
param=paramPredi;
end
for j=1:p; eval([ParamResul(j,:) '(k,:)=param(j,:);']); end;
if k>k1;
eval([ParamResul(1,:) '(k,:)=normrnd(param(1,:),param(end,:));']);
end
end
% % % POST-PROCESSING
time=[0:dt:dt*(k-1)]'; % RUL Calculation
perceValue=[50 signiLevel 100-signiLevel];
for i=1:n;
loca=find(eval([ParamResul(1,:) '(:,i)'])*cofec>=thres*cofec);
RUL(i)=time(loca(1))-time(k1);
end;
RULPrece=prctile(RUL',perceValue);
figure; set(gca,'fontsize',14); hist(RUL,30); % RUL Results Display
xlim([min(RUL) max(RUL)]);
xlabel(['RUL' ' (' TimeUnit ')']);
titleName=['at ' num2str(time(k1)) ' ' TimeUnit]; title(titleName)
fprintf('\n # Percentiles of RUL atg weeks \n', time(k1))
fprintf('\n %g, median: %g, %gprct: %g \n', perceValue(2), ...
RULPerce(2), RULPerce(1), perceValue(3), RULPerce(3))
Name=[WorkName ' at ' num2str(time(k1)) '.mat']; save(Name); % Work Save
The following is the error that appears in the command window
>> Prognosis
Error using Prognosis
Invalid use of operator.
  1 commentaire
Sarvesh Kale
Sarvesh Kale le 9 Fév 2023
The error is in the following line
p=size (ParamName,1);
for j=1:p;
param(j,:)=unifrnd(initDisPar(j,1),initDisPar(j,2),1,n);
ParamResul(j,:)=[ParamName(j,:) 'Resul'];
eval([ParamResul(j,:) '=param(j,:);']); % this line gives error
end;
what are you exactly trying to do with the eval there ?

Connectez-vous pour commenter.

Réponses (1)

Jan
Jan le 9 Fév 2023
Modifié(e) : Jan le 9 Fév 2023
The old problem: Strange methods to define names of variables dynamically using eval() demands for even stranger methods to access these variables later on. This is the typical shot in the own knee. See: FAQ: Why and how to avoid EVAL .
Use dynamic field names instead:
% Replace:
% ParamResul(j,:)=[ParamName(j,:) 'Resul'];
% eval([ParamResul(j,:) '=param(j,:);']);
% by:
Resul.(ParamName(j)) = param(j,:);
Afterwards all eval calls can be replaced by using the fields directly. This makes the code cleaner, easier to debug and to maintain and much faster.
  3 commentaires
Jan
Jan le 14 Fév 2023
I cannot fix the errors in your code without seeing the code. So please post zu current version of your code.
Changing
eval([ParamResul(j,:) '=param(j,:);']);
to
Resul.(ParamName(j)) = param(j,:);
seems to be more or less trivial, so I cannot guess, which problems you have with it.
Ikhsan Romli
Ikhsan Romli le 15 Fév 2023
Here I attach my version of the code

Connectez-vous pour commenter.

Catégories

En savoir plus sur Deploy Predictive Maintenance Algorithms dans Help Center et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by