Data conversion: Data type cell to char within a cell array

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

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?
Tim
Tim 2020년 12월 4일
편집: Tim 2020년 12월 4일
Excuse my typo, I transcribed my code from another computer. Those lines should read the following.
x = char(a{j}{i,1});
y = char(a{j}{i,3});
a{j}{i,1} = x;
a{j}{i,3} = y;
The script this code is used in takes a lengthy text file and parses it. The 15 separate variabes are 15 different states. I do agree there's definitely a better way to organize the data.
Here's a shortened example of the data.
cell_array1 = {{'a'}, 1, {'b'}, 2, 'c'; {'d'}, 3, {'e'}, 4, 'f'}
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.
c= {{'a'},1,{'b'},2,'d';{'c'},4,{'d'},5,6};
d= {{'e'},7,{'f'},8,'f';{'g'},10,{'h'},11,12};
e = {c, d};
for j = 1: length(e)
for i = [1,3]
e{j}(:,i) = arrayfun(@cellstr,cellfun(@char,e{j}(:,i)));
end
end
If I set up my code like this, e{1} or e{2} gives me the cell arrays properly formatted. But the goal is for 'c' and 'd' formatted in the manner.
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.
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');

댓글을 달려면 로그인하십시오.

답변 (0개)

카테고리

도움말 센터File Exchange에서 Logical에 대해 자세히 알아보기

질문:

Tim
2020년 12월 4일

댓글:

2020년 12월 4일

Community Treasure Hunt

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

Start Hunting!

Translated by