嵌套单元格到字符串

Nested Cell to string

我有以下问题:

Objective(高级): 我想将 ESRI Shapefile 转换为 SQL 空间数据。为此,我需要调整语法。

当前状态/问题: 我构建了以下单元格数组:

'MULTIPOLYGON('    {1x2332 cell}    ','    {1x916 cell}    ','    {1x391 cell}    ','    {1x265 cell}    ')'

总共有 9 个字段。此元胞数组包含以下 'nested' 个元胞数组:{1x2332 元胞}、{1x916 元胞}、{1x391 元胞}、{1x265 元胞}。例如,'nested' 单元格 {1x2332 单元格} 具有以下形式:

'(('    [12.714606000000000]    [42.155628000000000]    ','    [12.702529999999999]    [42.152873999999997]    ',' ... ','    [12.714606000000000]    [42.155628000000000]    '))'

但是,我希望将整个单元格数组(包括所有 'nested cells')作为一个字符串,不带任何 spaces(数字(坐标)之间的 space 除外) ).您知道我如何找到解决方案吗?

提前致谢。

你可能需要循环。

考虑一个更小的例子:

innerCell1 = {'((' [12.714606000000000] [42.155628000000000] ',' [12.702529999999999] [42.152873999999997] ',' [12.714606000000000] [42.155628000000000] '))'};
outerCell = {'MULTIPOLYGON(' innerCell1 ',' innerCell1 ')'};

您可以沿着这些路线走:

outer = outerCell; %// will be overwritten
ind_outer = find(cellfun(@iscell, outer)); %// positions of inner cell arrays in `outer`
for m = ind_outer
    inner = outer{m};
    ind_inner = cellfun(@isnumeric, inner); %// positions of numbers in `inner`
    ind_inner_space = find(ind_inner(1:end-1) & ind_inner(2:end)); %// need space
    ind_inner_nospace = setdiff(find(ind_inner), ind_inner_space); %// don't need
    for n = ind_inner_space
        inner{n} = [num2str(inner{n}) ' ']; %// convert to string with space
    end
    for n = ind_inner_nospace
        inner{n} = num2str(inner{n}); %// convert to string, without space
    end
    outer{m} = [inner{:}]; %// concatenate all parts of `inner`
end
str = [outer{:}]; %// concatenate all parts of `outer`

这导致字符串

str =
MULTIPOLYGON(((12.7146 42.1556,12.7025 42.1529,12.7146 42.1556)),((12.7146 42.1556,12.7025 42.1529,12.7146 42.1556)))