Data conversion: Data type cell to char within a cell array
Show older comments
I have 15 cell arrays, size 1000+ x 5 and two of the columns need to be converted from data type cell to char.
This for loop works without any issues.
for i = 1: length(cell_array1)
x = char(cell_array1{i,1});
y = char(cell_array1{i,3});
cell_array1{i,1} = x;
cell_array1{i,3} = y;
end
However, when I adjust the for loop to loop through 15 different cell arrays, it returns data type cell in both columns I need to change to char.
a = {cell_array1, cell_array2};
for j = 1: length(a)
for i = 1: length(a{j})
x = char(a{i,1});
y = char(a{i,3});
a{j}{i,1} = x;
a{j}{i,3} = y;
end
end
6 Comments
dpb
on 4 Dec 2020
x = char(a{i,1});
y = char(a{i,3});
a{j}{i,1} = x;
a{j}{i,3} = y;
You're not referencing the same in the first two lines as the second two...
Looks very inefficient; what's the reason for the large number of different arrays -- if you actually have 15 separate variables as the above catenation of the second would seem to imply, this is a VERY tedious and code-inefficient way to to things to have to write the same code over to reference those variables. Having to do that generally indicates a better way to organize the data probably exists.
How about attach a shortened sample version for folks to play with? Three of 10x5 is just as effective in illustrating as are 15 @ 1000x5.
Also, the better solution to the problem would be to fix the issue when these are created, not have to patch them up later. How were the arrays built to begin with?
dpb
on 4 Dec 2020
It can be a pain to dereference...there's not a builtin command to "unnest" a level of indirection.
For the above array, can do the same thing your explicit loop does a little more obscurely, perhaps, but with somewhat less overall code --
c=cell_array1; % just use a shorter variable name
for i=[1,3]
c(:,i)=arrayfun(@cellstr,cellfun(@char,c(:,i)));
end
>> c
c =
2×5 cell array
{'a'} {[1]} {'b'} {[2]} {'c'}
{'d'} {[3]} {'e'} {[4]} {'f'}
>>
"this code is used in takes a lengthy text file and parses it."
I'd submit it's time to look into that part of the code first. Attach enough of one of those with the reading code and somebody can probably help there and avoid the above cleanup.
Tim
on 4 Dec 2020
dpb
on 4 Dec 2020
That's the problem with having built separate variables..."there be dragons!"
If you have separate variables to operate on, then you have to write code to address those variables. There's no point in putting them in e if you're not going to use it instead; just operate on c, d individually. Of course, when you do, then you'll probably have to continue to duplicate going forward to do the same thing on both.
As said, I think it's time to revisit the beginning of how you got yourself into the mess and extricate yourself therefrom back there rather than following this path.
Image Analyst
on 4 Dec 2020
Attach your cell arrays in a .mat file if you have any more problems (so we can quite guessing).
save('answers.mat', 'cell_array1', 'cell_array2');
Answers (0)
Categories
Find more on Logical in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!