How to get string field from struct with each element separated

6 ビュー (過去 30 日間)
Adel Sarhan
Adel Sarhan 2019 年 5 月 11 日
コメント済み: Rik 2023 年 5 月 26 日
>> x.name
ans =
'1.33'
ans =
'1.34'
ans =
'1.35'
>> y = [x.name]
y =
'1.331.341.35'
I want to get it like that
y = ['1.33' '1.34' '1.35']
as a matrix with three string elements
  2 件のコメント
Rik
Rik 2023 年 5 月 26 日
And if a string vector is a true requirement, the conversion is easy:
x = struct('name',{'1.33','1.34','1.35'}) % reconstruct the data from OP
x = 1×3 struct array with fields:
name
y = {x.name}
y = 1×3 cell array
{'1.33'} {'1.34'} {'1.35'}
z = string(y)
z = 1×3 string array
"1.33" "1.34" "1.35"

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

採用された回答

gonzalo Mier
gonzalo Mier 2019 年 5 月 11 日
The problem is you are using char instead of string. '1.33' is a vector of char, so if you make a vector of vectors, it compiles them in a row. To make them string you can write "1.33" instead of '1.33' or string('1.33').
  1 件のコメント
gonzalo Mier
gonzalo Mier 2019 年 5 月 11 日
for i= 1:length(x)
y(i) = string(x(i).name);
end

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

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeString Parsing についてさらに検索

タグ

製品


リリース

R2018a

Community Treasure Hunt

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

Start Hunting!

Translated by