Error: This statement is incomplete.

19 次查看(过去 30 天)
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 个评论
Stephen23
Stephen23 2021-10-21
编辑:Stephen23 2021-10-21
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.

请先登录,再进行评论。

采纳的回答

Dave B
Dave B 2021-10-21
编辑:Dave B 2021-10-21
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 个评论
Dave B
Dave B 2021-10-21
@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)...

请先登录,再进行评论。

更多回答(2 个)

Star Strider
Star Strider 2021-10-21
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 个评论
Stephen23
Stephen23 2021-10-21
编辑:Stephen23 2021-10-21
+1 for the best advice on this page, with a helpful and efficient example.

请先登录,再进行评论。


Voss
Voss 2021-10-21
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.

类别

Help CenterFile Exchange 中查找有关 Logical 的更多信息

产品


版本

R2019a

Community Treasure Hunt

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

Start Hunting!

Translated by