How to Convert Cell Array contents to look like they do when I OPEN the variable it the MatLab Variable Viewer

조회 수: 2 (최근 30일)
I've got a cell array, TheDatabaseInfo, that I'm trying to load into a UITABLE (in the 'Data' portion of the call) but I'm getting an error because some of the elements of the cell array are multi-dimensional. In the example show here, element 16 is a vertical array [0;8;0;0] and, so, the UITABLE operation fails.
My ideal solution would be to alter the cell array so that it looks just like it does when I view the contents in the MatLab Variable Viewer (see image). For example, replace element 16 with a string '[0;8;0;0]'. And, of course, do that with any other cell array elements that could cause an error when creating my table.
Any ideas?

채택된 답변

Jan
Jan 2016년 3월 30일
You can convert the elements of the array manually:
Data = {1, [1;2;3;4], 1:2, 'String', {'cell'}};
S = cell(size(Data));
for k = 1:numel(Data)
C = Data{k};
if isnumeric(C)
if length(C) == 1
S{k} = sprintf('%g', C);
elseif isvector(C)
if length(C) < 4 % For example
if size(C, 1) == 1
S{k} = ['[', sprintf('%g,', C(1:end-1)), sprintf('%g]', C(end))];
else
S{k} = ['[', sprintf('%g;', C(1:end-1)), sprintf('%g]', C(end))];
end
end
end
elseif ischar(C)
S{k} = C;
elseif iscell(C)
tmp = sprintf('%d,', size(C));
S{k} = ['{', tmp(1:end-2), '} cell'];
end
if isempty(S{k}) % Fallback if other methods did not match:
tmp = sprintf('%d,', size(C));
S{k} = ['[', tmp(1:end-2), '] ', class(C)];
end
end
This can be improved in a lot of ways: Shorten too long outputs, explicit output of short cell strings, include the type of integer classes, flexible display of digits after the decimal point for floating point values, etc.

추가 답변 (1개)

Dwight Bartholomew
Dwight Bartholomew 2016년 3월 30일
Thanks Jan, That worked GREAT!

카테고리

Help CenterFile Exchange에서 Data Type Conversion에 대해 자세히 알아보기

태그

제품

Community Treasure Hunt

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

Start Hunting!

Translated by