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?
1 件のコメント
Jan
2016 年 3 月 30 日
Please provide a small test array, preferrably in Matlab syntax, such that we can use it by copy&paste.
採用された回答
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.
0 件のコメント
その他の回答 (1 件)
参考
カテゴリ
Help Center および File Exchange で Data Type Conversion についてさらに検索
製品
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!