Filter löschen
Filter löschen

Error: This statement is incomplete.

17 Ansichten (letzte 30 Tage)
Lennard Pol
Lennard Pol am 21 Okt. 2021
Kommentiert: Dave B am 21 Okt. 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 Kommentar
Stephen23
Stephen23 am 21 Okt. 2021
Bearbeitet: Stephen23 am 21 Okt. 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.

Melden Sie sich an, um zu kommentieren.

Akzeptierte Antwort

Dave B
Dave B am 21 Okt. 2021
Bearbeitet: Dave B am 21 Okt. 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 Kommentare
Lennard Pol
Lennard Pol am 21 Okt. 2021
Thanks Dave!
Dave B
Dave B am 21 Okt. 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)...

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (2)

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

Melden Sie sich an, um zu kommentieren.


Voss
Voss am 21 Okt. 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.

Produkte


Version

R2019a

Community Treasure Hunt

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

Start Hunting!

Translated by