How do I add a cell array of strings to a field in a structure and save it to a shapefile?

1 view (last 30 days)
I want to edit a shapefile and add a new field that contains a string that is a textual description of each data point "(X,Y)" in the structure.
The code below illustrates what I want.
S = shaperead('concord_roads.shp')
j=1
for i=1:numel(S)
S(i).NewName = repmat({num2str(j)}, 1, numel(S(i).X));
j=j+1;
end
This works fine, however, when I try to save the new structure to a shapefile as follows
% Save the file
shapewrite(S,'test');
I get the warning:
Warning: Omitting unsupported data class: cell
And the saved shapefile does not contain my new field.
Please explain why this is happening and how I can achieve what I want.

Accepted Answer

MathWorks Support Team
MathWorks Support Team on 25 Jun 2020
Edited: MathWorks Support Team on 25 Jun 2020
The reason why you get this warning and why the field “newName” is not saved in the new shapefile is because it is an object of the class cell. You can check this by typing the following command in the Command Window:
class(S(1).NewName)
The fields in a structure that you want to write to a shapefile must be a scalar of class double or a character string. This limitation is due to the external shapefile format itself, and not a restriction imposed by MathWorks. This information and additional details on the expected format for a shapefile can be found in the documentation page for the “shapewrite” function at the link below:
Since the field “NewName” is of class cell, the “shapewrite” function simply ignores the field and returns a warning.
To work around this issue and achieve the same desired behavior, the textual information can be stored in the form of a string with a delimiter of your choice. In the example below, I used a coma as the delimiter between the different field descriptors:
S = shaperead('concord_roads.shp');
for i=1:numel(S)
S(i).NewName = repmat([num2str(j) ','], 1, numel(S(i).X));
j=j+1;
end
shapewrite(S,'test');
Then, the string can easily be parsed using the “textscan” function which returns a cell. For instance:
VarNames = textscan(S(1).NewName,'%s', 'delimiter', ',');
VarNames = VarNames{1};
where the delimiter in the example above is expected to be a coma.
For more information about the “textscan” function and its input and output arguments, please refer to the documentation page below:

More Answers (0)

Tags

No tags entered yet.

Products


Release

R2013b

Community Treasure Hunt

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

Start Hunting!