Converting a comma separated list into an array concatenates them.

조회 수: 123 (최근 30일)
Arya Das
Arya Das 2020년 6월 19일
답변: Captain Karnage 2023년 3월 29일
I was trying to convert a comma separated list of strings into a normal array. So B0005.cycle is a struct with some fields:
B0005.cycle.type is a comma separated list:
I tried to convert it into an array by doing this: [B0005.cycle.type] but it gives this output:
However I want ["charge", "discharge","charge","discharge",...]. So how do I go about this?
I have attached the mat file below.

채택된 답변

Atul Upadhyay
Atul Upadhyay 2020년 6월 19일
I can see that you are trying to convert a comma separated list of strings to normal array. You can do it like following.
arr={B0005.cycle.type};
newarr=[];
for x=arr
newarr=[newarr; string(x{1})];
end
Refer to the following documentation to know more about comma-separated lists in MATLAB.

추가 답변 (1개)

Captain Karnage
Captain Karnage 2023년 3월 29일
This can also be done in one line (using type for an example):
If you want a horizontal array:
typearr = string({B0005.cycle.type});
If you want a vertical array:
typearr = string({B0005.cycle.type}');
{} will convert the comma separated list of char to cell array of char
' will transpose the cell array
string will convert cell arrays of char to arrays of string

카테고리

Help CenterFile Exchange에서 Data Type Conversion에 대해 자세히 알아보기

태그

제품


릴리스

R2020a

Community Treasure Hunt

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

Start Hunting!

Translated by