Borrar filtros
Borrar filtros

Error: This statement is incomplete.

17 visualizaciones (últimos 30 días)
Lennard Pol
Lennard Pol el 21 de Oct. de 2021
Comentada: Dave B el 21 de Oct. de 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 comentario
Stephen23
Stephen23 el 21 de Oct. de 2021
Editada: Stephen23 el 21 de Oct. de 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.

Iniciar sesión para comentar.

Respuesta aceptada

Dave B
Dave B el 21 de Oct. de 2021
Editada: Dave B el 21 de Oct. de 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 comentarios
Lennard Pol
Lennard Pol el 21 de Oct. de 2021
Thanks Dave!
Dave B
Dave B el 21 de Oct. de 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)...

Iniciar sesión para comentar.

Más respuestas (2)

Star Strider
Star Strider el 21 de Oct. de 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 comentarios
Stephen23
Stephen23 el 21 de Oct. de 2021
Editada: Stephen23 el 21 de Oct. de 2021
+1 for the best advice on this page, with a helpful and efficient example.
Star Strider
Star Strider el 21 de Oct. de 2021
@Stephen Thank you!
.

Iniciar sesión para comentar.


Voss
Voss el 21 de Oct. de 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.

Categorías

Más información sobre Logical en Help Center y File Exchange.

Productos


Versión

R2019a

Community Treasure Hunt

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

Start Hunting!

Translated by