Change type of a structure field
17 次查看(过去 30 天)
显示 更早的评论
I'm fairly new to Matlab, so this might be a simple question or one that just isn't possible. I have a bunch of data that was grabbed from a JSON file. For one particular field, there might have been one or more words in the JSON object for any given item. The problem is that the toolbox I used (JSONlab) made that field a character array if there was only one word, and a cell array if there were more than one word. I would like all of the fields for each item to be cell array, regardless of the number of words in that field. Is there a way to force character arrays to become cell arrays?
Here's what I've tried so far (data is a 1x6000 cell array with 5556 valid items):
for i = 1:5556
item = data{1,i};
categories = item.categories;
if ischar(categories)
item.categories = cellstr(categories);
end
end
This doesn't have any compile/run-time issues, but it doesn't actually change anything. For example, if the only thing in categories is "Mexican", then it's still a character array afterwards.
0 个评论
采纳的回答
dpb
2014-5-17
Does this help?
>> data{1}={'this is stuff'};
>> data{2}='word'
data =
{1x1 cell} 'word'
>> iscell(data{2})
ans =
0
>> iscell(data{1})
ans =
1
>> data(2)={data(2)}
data =
{1x1 cell} {1x1 cell}
>>
?
2 个评论
dpb
2014-5-17
BTW, note the following for the above initial data array--
>> isnt=~cellfun(@iscell,data);
>> data(isnt)={data(isnt)}
data =
{1x1 cell} {1x1 cell}
May make your job a little simpler.
更多回答(0 个)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 JSON Format 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!