フィルターのクリア

How to convert a 1x1 table coining a list of N names into a Nx1 string of names

2 ビュー (過去 30 日間)
Nicola Scafetta
Nicola Scafetta 2024 年 5 月 28 日
コメント済み: Dyuman Joshi 2024 年 5 月 28 日
I have a 1x1 table T element which contains several names as the following
T = { 'Aaaa; Bbbb; Cccc; Dddd' }
I would like to convert it into a 4x1 string array of 4 names as
TT = ["Aaaa" ; "Bbbb" ; "Cccc" ; "Dddd"]
so that TT(1)="Aaaa", etc.
how can I do it?

回答 (1 件)

Lokesh
Lokesh 2024 年 5 月 28 日
編集済み: Lokesh 2024 年 5 月 28 日
Hi Nicola,
It appears that T is a cell array containing a single string. To achieve the conversion to a 4x1 string array 'TT' as described, you can follow these steps in MATLAB:
  1. Extract the string from the cell array: First, access the string stored in the cell array 'T'.
  2. Split the string: Use the 'split' function to split the string into parts based on the semicolon delimiter. Since the input is a character vector, the output of 'split' will be a cell array of character vectors.
  3. Convert to a string array: Finally, convert the cell array of character vectors to a string array.
Here's how you can do it:
% Given cell array T
T = {'Aaaa; Bbbb; Cccc; Dddd'};
% Extract the string from the cell array
str = T{1};
% Split the string into parts based on the semicolon
parts = split(str, '; ');
% Convert the cell array of character vectors to a string array
TT = string(parts);
% If you need TT to be a 4x1 column vector explicitly
TT = reshape(TT, [], 1);
Refer to the following documentation to know more about 'split':
  1 件のコメント
Dyuman Joshi
Dyuman Joshi 2024 年 5 月 28 日
Your code has a bit of redundancy. Here's a cleaned up version -
T = {'Aaaa; Bbbb; Cccc; Dddd'};
out = string(split(T, '; '))
out = 4x1 string array
"Aaaa" "Bbbb" "Cccc" "Dddd"
Check the output after each operation you perform and you will catch the redundancies.

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

Community Treasure Hunt

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

Start Hunting!

Translated by