Converting a comma separated list into an array concatenates them.
129 vues (au cours des 30 derniers jours)
Afficher commentaires plus anciens
Arya Das
le 19 Juin 2020
Réponse apportée : Captain Karnage
le 29 Mar 2023
I was trying to convert a comma separated list of strings into a normal array. So B0005.cycle is a struct with some fields:
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/318979/image.png)
B0005.cycle.type is a comma separated list:
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/318982/image.png)
I tried to convert it into an array by doing this: [B0005.cycle.type] but it gives this output:
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/318985/image.png)
However I want ["charge", "discharge","charge","discharge",...]. So how do I go about this?
I have attached the mat file below.
0 commentaires
Réponse acceptée
Atul Upadhyay
le 19 Juin 2020
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.
0 commentaires
Plus de réponses (1)
Captain Karnage
le 29 Mar 2023
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
0 commentaires
Voir également
Catégories
En savoir plus sur Data Type Conversion dans Help Center et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!