I have 3 strings like the following:
world=["America", "Europe"]; % Parent String
America=["USA", "Brazil", "Argentina"]; % Child1 String
Europe=["France", "Germany", "England"]; % Child2 String
I want to access the elements of the child string (Country names) from the Parent string(world) and use it inside a for loop for further operation. I am trying in the following way:
for i=1:2
d=world(i); % Temporary variable to store the continent name
for j=1:3
disp(d(j)); % Throws an Error: "Index exceeds Matrix dimensions
end
end
What am I doing wrong? Any help will be highly appreciated.

2 件のコメント

KL
KL 2017 年 10 月 17 日
You're defining three entirely different variables. Where and how do you want to apply this?
Stephen23
Stephen23 2017 年 10 月 17 日
The simplest and most efficient solution is to use structures and dynamic fields:
Do not try to magically access variable names. Doing so will make your code slow, complex, buggy, hard to debug, inscure. Read this to know why:

サインインしてコメントする。

 採用された回答

Cedric
Cedric 2017 年 10 月 17 日
編集済み: Cedric 2017 年 10 月 17 日

3 投票

Here is one way, as string arrays are "iterable".
continents = ["America", "Europe"] ;
countries.America = ["USA", "Brazil", "Argentina"] ;
countries.Europe = ["France", "Germany", "England"] ;
for continent = continents
fprintf('%s :\n', continent ) ;
for country = countries.(continent)
fprintf('\t%s\n', country) ;
end
end
Yet, using dynamic field names for that is usually not advised. You could use cell arrays instead:
continents = {"America", "Europe"} ;
countries{1} = {"USA", "Brazil", "Argentina"} ;
countries{2} = {"France", "Germany", "England"} ;
for continentId = 1 : numel( continents )
fprintf('%s :\n', continents{continentId} ) ;
for countryId = 1 : numel( countries{continentId} )
fprintf('\t%s\n', countries{continentId}{countryId} ) ;
end
end

9 件のコメント

Stephen23
Stephen23 2017 年 10 月 17 日
編集済み: Stephen23 2017 年 10 月 17 日
" using dynamic field names for that is usually not advised"
I have never read anything in the MATLAB documentation that advises against using dynamic field names, nor any TMW staff advising against them. Can you explain why that would be a bad way to solve this task? As far as I can see, dynamic fieldnames allow for a very neat solution to the original question.
Cedric
Cedric 2017 年 10 月 17 日
Because continent names must be compatible with field name requirements as you know. I am generally not advising using data as dynamic field names until one gets enough proficiency, because unless it is managed well it is often a source of problems.
Pradeep: in your particular case, the code may work well but as soon as someone wants to update the list of continents and differentiate "South America" from "North America" (using a space), it will crash (unless you implement proper field name normalization).
Stephen23
Stephen23 2017 年 10 月 17 日
True, it does break the boundary separating data and code. Pity, as otherwise it is a neat solution.
pradeep kumar
pradeep kumar 2017 年 10 月 17 日
@ Cedric Wannaz; Thanks for your quick response. However it produces the following error
America :
Dynamic field or property name must be a character vector.
Cedric
Cedric 2017 年 10 月 17 日
編集済み: Cedric 2017 年 10 月 17 日
Well I still think that this is a neat solution, and as you say in your comment it is simple and efficient. My point was more about the pedagogy; I usually tell people who start programming not to use data as dynamic field names until they fully understand what they are doing (and are technically capable of implementing the necessary normalization/tests).
pradeep kumar
pradeep kumar 2017 年 10 月 17 日
@ Cedric Wannaz: The second part of your solution runs perfect and does my job. I have just started doing MATLAB. Thank You a lot!
Cedric
Cedric 2017 年 10 月 17 日
編集済み: Cedric 2017 年 10 月 17 日
Pradeep, which version of MATLAB are you using, it is < 2016b ? Do you really need to use strings by the way? Does the following work?
continents = ["America", "Europe"] ;
countries.America = ["USA", "Brazil", "Argentina"] ;
countries.Europe = ["France", "Germany", "England"] ;
for continent = continents
fprintf('%s :\n', continent ) ;
for country = countries.(char(continent))
fprintf('\t%s\n', country) ;
end
end
pradeep kumar
pradeep kumar 2017 年 10 月 17 日
Cedric, The above solution works perfect. I am using MATLAB 2017a. You are genius. Thanks a lot. I hope, You were my TEACHER here.
Cedric
Cedric 2017 年 10 月 17 日
編集済み: Cedric 2017 年 10 月 17 日
Awesome. Thank you for the positive feedback!

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

ヘルプ センター および File ExchangeStartup and Shutdown についてさらに検索

製品

質問済み:

2017 年 10 月 17 日

編集済み:

2017 年 10 月 17 日

Community Treasure Hunt

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

Start Hunting!

Translated by