separate a variable in a struct

6 ビュー (過去 30 日間)
Carmo
Carmo 2022 年 12 月 30 日
コメント済み: Steven Lord 2022 年 12 月 30 日
Hello, I am having a problem separating a variable I created that is part of a struct like
A.PatientName=([struct{4,1} struct{5,1} struct{6,1}])
It shows like 'FlorbelaLopesFerreiro' and not Florbela Lopes Ferreiro. Any solutions?

回答 (1 件)

Voss
Voss 2022 年 12 月 30 日
I guess that struct is a variable you created, that struct is a cell array, that struct{4,1} is the character vector 'Florbela', that struct{4,2} is the character vector 'Lopes', and that struct{4,3} is the character vector 'Ferreiro'.
You shouldn't name a variable "struct" because that conflicts with the built-in MATLAB function "struct". You won't be able to use the function "struct" as long as the variable "struct" is in the workspace. So I'll call it "S". Maybe it looks like this:
S = {'Some';'Other';'Name';'Florbela';'Lopes';'Ferreiro'}
S = 6×1 cell array
{'Some' } {'Other' } {'Name' } {'Florbela'} {'Lopes' } {'Ferreiro'}
If you want spaces between the words, you can do this:
A.PatientName = [S{4,1}, ' ', S{5,1}, ' ', S{6,1}]
A = struct with fields:
PatientName: 'Florbela Lopes Ferreiro'
or this:
A.PatientName = sprintf('%s %s %s',S{4:6,1})
A = struct with fields:
PatientName: 'Florbela Lopes Ferreiro'
  1 件のコメント
Steven Lord
Steven Lord 2022 年 12 月 30 日
Or use strjoin.
S = {'Some';'Other';'Name';'Florbela';'Lopes';'Ferreiro'};
fullname = strjoin(S(4:6))
fullname = 'Florbela Lopes Ferreiro'

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

カテゴリ

Help Center および File ExchangeCell Arrays についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by