How to get string field from struct with each element separated

>> 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

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일

0 개 추천

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').

추가 답변 (0개)

카테고리

도움말 센터File Exchange에서 Event Functions에 대해 자세히 알아보기

제품

릴리스

R2018a

태그

질문:

2019년 5월 11일

댓글:

Rik
2023년 5월 26일

Community Treasure Hunt

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

Start Hunting!

Translated by