How to convert a char array field in a struct array to a string field in a vectorized fashion.

103 views (last 30 days)
William Bahn
William Bahn on 29 Oct 2021
Commented: William Bahn on 29 Oct 2021
I have a struct array in which one of the fields is a character array. I need to convert it to a string scalar for all stuctures in the array. I have been unable to find a way to do this in a vectorized manner.
myArray = [struct("code", 'CO128'), struct("code", 'TX457'];
This is a toy data structure -- the real thing has dozens of fields in each element and tens of thousands of elements in the array. I have no control over the array format up to this point.
What I am trying to accomplish is the same thing that is performed by the following loop:
for i = 1:size(myArray,2)
myArray(i).code = string(myArray(i).code);
I am restricted to the functionality of the basic MATLAB R2021a installation (no optional toolboxes).

Accepted Answer

Stephen23 on 29 Oct 2021
S = struct('code',{'CO128','TX457'});
S.code % checking
ans = 'CO128'
ans = 'TX457'
tmp = num2cell(string({S.code}));
[S.code] = tmp{:};
S.code % checking
ans = "CO128"
ans = "TX457"
  1 Comment
William Bahn
William Bahn on 29 Oct 2021
Thank you!
For others that come across this, here is my explanation of how this works:
myCellArray = {S.code};
S.code evaluates to a comma separated list and the curly braces then pack this into a cell array with each cell containing the contents of the corresponding the code field of the corresponding element of S.
myStringArray = string(myCellArray);
Converts each cell into a string. Requires that all cells contain content that can be converted to a string scalar, including char arrays and strings, as well as numerical and logical scalars.
myStringCellArray = num2cell(myStringArray);
Converts an array of ANY data type to a cell array, preserving the data type in the corresponding cell.
[S.code] = myStringCellArray{:};
The RHS produces a comma-separated list.
The LHS is a comma-separate list of target locations.
Now for my observations:
This is clever, and tricky, and very non-intuitive (which is not to say that it isn't the cleanest and simplist way to accomplish this task).
This seems like such a basic and common task that there should be support for it in a much more direct way, such as simple casting or perhaps:
convert(S.code like "string")
The big stumbling block for me in figuring something out was the very misleading naming convention that is so common in MATLAB functions. I didn't even look at num2cell because I wasn't converting numbers to anything, so it didn't seem to apply.
I managed to get to an array of strings (i.e., string({S.code}) ) but couldn't figure out how to get that array copied over into the structure array. I kept getting error messages suggesting that I use a comma-separated list, but could never find out how to convert an array into a comma separated list. I knew I could evaluate a cell array (or a structure field) as a comma-separated list, but I couldn't figure out how to convert an array of strings to a cell array of strings. The cellstr() function was almost what I needed, except that it insists on converting things not to strings, as the name would imply, but to character arrays, which was precisely the opposite of what I was trying to do and merely took me back right where I started.

Sign in to comment.

More Answers (1)

Fangjun Jiang
Fangjun Jiang on 29 Oct 2021
It can be done but not sure if it is more efficient than the simple for-loop

Community Treasure Hunt

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

Start Hunting!