store strings and numbers into an array

4 ビュー (過去 30 日間)
Youssef Darwich
Youssef Darwich 2022 年 1 月 8 日
コメント済み: Youssef Darwich 2022 年 1 月 8 日
hello everybody,
i'am still a beginner in MATLAB and need your help.
if i have a list of Person names and their ages. e.g. Peter: 28; Fabian: 21; Mary: 32.
how to create an array in MATLAB including these informations and how to access them on the base of the name or age.
I appreciate every help.
thank you

採用された回答

Rik
Rik 2022 年 1 月 8 日
I would suggest create a struct array:
data=struct('Name',{'Peter','Fabian','Mary'});
[data.Age]=deal(28,21,32);%just to show a different syntax
L=ismember({data.Name},'Peter');
data(L).Age
ans = 28
  1 件のコメント
Youssef Darwich
Youssef Darwich 2022 年 1 月 8 日
thank you this was very helpful :)

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

その他の回答 (1 件)

Chunru
Chunru 2022 年 1 月 8 日
% You can use structure array
PersonInfo(1) = struct("Name", "Peter", "Age", 28);
PersonInfo(2) = struct("Name", "Fabian", "Age", 21);
PersonInfo(3) = struct("Name", "Mary", "Age", 32);
PersonInfo(1)
ans = struct with fields:
Name: "Peter" Age: 28
  1 件のコメント
Youssef Darwich
Youssef Darwich 2022 年 1 月 8 日
thank you that helped me too :)

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

カテゴリ

Help Center および File ExchangeCharacters and Strings についてさらに検索

製品


リリース

R2020a

Community Treasure Hunt

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

Start Hunting!

Translated by