Error: This statement is incomplete.

21 vues (au cours des 30 derniers jours)
Lennard Pol
Lennard Pol le 21 Oct 2021
Commenté : Dave B le 21 Oct 2021
Hi all,
I keep facing: 'Error: This statement is incomplete.'. I am using a simple for-loop to create 12 variables out of previous ones.
I have several matrices of the 'double' type', that are named "year_2017_j", with j from 1 to 12 (resembling months).
The inner function (in purple) works, but something goes wrong with the complete function.
for j = 1:12
eval(sprintf('factor_2017_%d = trapz(year_2017_%d(:,4))/trapz(year_2017_%d(:,3));', j));
end
Thanks,
Lennard
  1 commentaire
Stephen23
Stephen23 le 21 Oct 2021
Modifié(e) : Stephen23 le 21 Oct 2021
Ugh.
Do not do that.
Putting meta-data (e.g. dates) into variables names is a sign that you are doing something wrong:
Better data design (e.g. using indexing rather than magically accessing variabale names and pointlessly obfuscating lots of code inside strings which then then to be evaluated) would make this bug much easier to identify and fix (in fact the MATLAB IDE would underline it and probably offer to fix it for you).
Instead of forcing meta-data into variable names (which forces you into writing slow, buggy, inefficient code (e.g. yours)) you would be much better off sytoring meta-data as data its own right. Then you are on your way to writing simpler, neater, much more efficient code.

Connectez-vous pour commenter.

Réponse acceptée

Dave B
Dave B le 21 Oct 2021
Modifié(e) : Dave B le 21 Oct 2021
MATLAB won't distribute your j to all the locations in your print string, so you need a few more js. Have a look without the eval or loop:
j=1;
sprintf('factor_2017_%d = trapz(year_2017_%d(:,4))/trapz(year_2017_%d(:,3));', j)
ans = 'factor_2017_1 = trapz(year_2017_'
sprintf('factor_2017_%d = trapz(year_2017_%d(:,4))/trapz(year_2017_%d(:,3));', j, j, j)
ans = 'factor_2017_1 = trapz(year_2017_1(:,4))/trapz(year_2017_1(:,3));'
  2 commentaires
Lennard Pol
Lennard Pol le 21 Oct 2021
Thanks Dave!
Dave B
Dave B le 21 Oct 2021
@Lennard Pol - I also wanted to note that I agree with @Stephen's comment...this isn't a great pattern and it'll always lead to bugs and hard-to-read code. Consider rewriting to not use numbers in your variables and instead store in an array (leverage a cell array if your variables are all different sizes)...

Connectez-vous pour commenter.

Plus de réponses (2)

Star Strider
Star Strider le 21 Oct 2021
I strongly advise against using eval.
Put all of the arrays into a cell array, and then address them appropirately —
year_2017_1 = randn(10,4);
year_2017_2 = randn(10,4);
year_2017_3 = randn(10,4);
year_2017_4 = randn(10,4);
year_2017_5 = randn(10,4);
year_2017_6 = randn(10,4);
year_2017_7 = randn(10,4);
year_2017_8 = randn(10,4);
year_2017_9 = randn(10,4);
year_2017_10 = randn(10,4);
year_2017_11 = randn(10,4);
year_2017_12 = randn(10,4);
year_2017 = {year_2017_1; year_2017_2; year_2017_3; year_2017_4; year_2017_5; year_2017_6; year_2017_7 ;year_2017_8 ; year_2017_9; year_2017_10; year_2017_11; year_2017_12};
factor_2017 = zeros(size(year(2017))); % Preallocate
for j = 1:12
factor_2017(j) = trapz(year_2017{j}(:,4)) / trapz(year_2017{j}(:,3));
end
.
  2 commentaires
Stephen23
Stephen23 le 21 Oct 2021
Modifié(e) : Stephen23 le 21 Oct 2021
+1 for the best advice on this page, with a helpful and efficient example.
Star Strider
Star Strider le 21 Oct 2021
@Stephen Thank you!
.

Connectez-vous pour commenter.


Voss
Voss le 21 Oct 2021
Pass j to sprintf three times instead of one:
for j = 1:12
eval(sprintf('factor_2017_%d = trapz(year_2017_%d(:,4))/trapz(year_2017_%d(:,3));', j, j, j));
end
since there are three %d's in the sprintf string and each one needs to be j.
  1 commentaire
Lennard Pol
Lennard Pol le 21 Oct 2021
Thanks Benjamin!

Connectez-vous pour commenter.

Catégories

En savoir plus sur Logical dans Help Center et File Exchange

Produits


Version

R2019a

Community Treasure Hunt

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

Start Hunting!

Translated by