필터 지우기
필터 지우기

Error: This statement is incomplete.

조회 수: 17 (최근 30일)
Lennard Pol
Lennard Pol 2021년 10월 21일
댓글: Dave B 2021년 10월 21일
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
Lennard Pol
Lennard Pol 2021년 10월 21일
Thanks Dave!
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.
Star Strider
Star Strider 2021년 10월 21일
@Stephen Thank you!
.

댓글을 달려면 로그인하십시오.


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