How to Convert Cell Array contents to look like they do when I OPEN the variable it the MatLab Variable Viewer
2 visualizzazioni (ultimi 30 giorni)
Mostra commenti meno recenti
Dwight Bartholomew
il 30 Mar 2016
Risposto: Dwight Bartholomew
il 30 Mar 2016
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 Commento
Jan
il 30 Mar 2016
Please provide a small test array, preferrably in Matlab syntax, such that we can use it by copy&paste.
Risposta accettata
Jan
il 30 Mar 2016
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 Commenti
Più risposte (1)
Vedere anche
Categorie
Scopri di più su Data Type Conversion in Help Center e File Exchange
Prodotti
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!