'for' loops in app designer methods have a parsing problem

조회 수: 2 (최근 30일)
Michael Cole
Michael Cole 2022년 11월 25일
답변: Image Analyst 2022년 11월 26일
I've been debugging a simple app designer project. I have a list of strings that are country names. I want to delete all duplicates so I have a simple property 'ii' that is an index for traversing the list so I referred to it as 'app.ii' in a 'for' loop that is inside a working method. Matlab won't let me do it and says there is a parsing error at '.' It will let me use 'app.ii' in a 'while' loop or 'if' statement but as soon as I type it in a 'for' loop a red line appears under the period. 'ii' is a declared property of the app. Anyone have an idea what this seemingly straightforward 'for' loop is doing wrong?

채택된 답변

Walter Roberson
Walter Roberson 2022년 11월 26일
It is a language limitation. for has never supported any kind of indexing for the loop control variable. The limitation saves defining a bunch of edge cases.
For example,
S(1).ii = 1;
S(2).ii = 7;
K = 1;
for S(K).ii = -2:2
S(K).ii
K = K + 1;
end
Does that record the value of K at the start of the loop and thereafter loop over S(1).ii ? Or should the second iteration be working with S(2).ii ?
  댓글 수: 1
Walter Roberson
Walter Roberson 2022년 11월 26일
Under what circumstances would it be relevant for a different part of the application need to know what the current for loop index is, such that making it a property of the application as a whole would be appropriate ?
Creating a loop index as a property of the application as a whole would give a tendency for people to want to change the loop control variable. If you have a different method that can change your active loop control index, then that is seldom a good design strategy.

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

추가 답변 (1개)

Image Analyst
Image Analyst 2022년 11월 26일
Try this to remove duplicates
% Remove duplicates from arrays of countries.
% Using character arrays.
countries = {'USA', 'Belgium', 'UK', 'USA', 'Japan'}
countries = 1×5 cell array
{'USA'} {'Belgium'} {'UK'} {'USA'} {'Japan'}
uniqueCountries = unique(countries)
uniqueCountries = 1×4 cell array
{'Belgium'} {'Japan'} {'UK'} {'USA'}
% Using strings.
countries2 = ["USA", "Belgium", "UK", "USA", "Japan"]
countries2 = 1×5 string array
"USA" "Belgium" "UK" "USA" "Japan"
uniqueCountries2 = unique(countries2)
uniqueCountries2 = 1×4 string array
"Belgium" "Japan" "UK" "USA"

카테고리

Help CenterFile Exchange에서 Logical에 대해 자세히 알아보기

제품


릴리스

R2022b

Community Treasure Hunt

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

Start Hunting!

Translated by