question about how to avoid using eval

1 visualizzazione (ultimi 30 giorni)
Baozai
Baozai il 22 Nov 2016
Commentato: Baozai il 22 Nov 2016
Hello, all.
I got to know that using "eval" is not efficient and may cause several problems. I have used "eval" a lot in my previous codes.
If I want to replace the following expression without using "eval", would anyone help me one this? Thank you very much.
for jjjj=1:5
for iiii=1:40
for kkkk=1:3
x1(kkkk,:) = (1:10)*kkkk;
eval(['Resp.Phi',num2str(jjjj),'.Mu',num2str(iiii),'.Respx1(kkkk,:) = x1(kkkk,:);']);
end
end
end

Risposta accettata

Walter Roberson
Walter Roberson il 22 Nov 2016
Resp.(sprintf('Phi%d',jjjj).(sprintf('Mu%d',iiii)),.Respx1(kkkk,:) = x1(kkkk,:);
  5 Commenti
Walter Roberson
Walter Roberson il 22 Nov 2016
You are right,
Resp.(sprintf('Phi%d',jjjj)).(sprintf('Mu%d',iiii)).Respx1(kkkk,:) = x1(kkkk,:);
Baozai
Baozai il 22 Nov 2016
This works, thank you very much.

Accedi per commentare.

Più risposte (1)

KSSV
KSSV il 22 Nov 2016
iwant = cell(5,40,3) ;
for jjjj=1:5
for iiii=1:40
for kkkk=1:3
x1(kkkk,:) = (1:10)*kkkk;
iwant{jjjj,iiii,kkkk}= ['Resp.Phi',num2str(jjjj),'.Mu',num2str(iiii),'.Respx1(kkkk,:) = x1(kkkk,:);'] ;
end
end
end
  3 Commenti
KSSV
KSSV il 22 Nov 2016
Modificato: KSSV il 22 Nov 2016
Resp = struct ;
for jjjj=1:5
for iiii=1:40
for kkkk=1:3
x1(kkkk,:) = (1:10)*kkkk;
% iwant{jjjj,iiii,kkkk}= ['Resp.Phi',num2str(jjjj),'.Mu',num2str(iiii),'.Respx1(kkkk,:) = x1(kkkk,:);'] ;
end
for llll = 1:3
Resp(jjjj).Phi1(iiii).Mu1(llll).Respx1 = x1 ;
end
end
end
Resp(1).Phi1(1).Mu1
Baozai
Baozai il 22 Nov 2016
This one also works, thank you very much for your help.

Accedi per commentare.

Categorie

Scopri di più su Programming in Help Center e File Exchange

Tag

Community Treasure Hunt

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

Start Hunting!

Translated by