String variables in uitable

Iam trying to create uitable with with integer and string columns (example code below) and iam getting error: Data must be a numeric, logical, or cell array. Is it possible to store string values in uitable? Or what are the options?
clear all;
fig = figure('Name','table','NumberTitle','off');
set(gcf, 'Units', 'Normalized', 'OuterPosition', [0 0 1 1]);
num1 = randn(3,1);
num2 = randn(3,1);
num3 = randn(3,1);
num4 = randn(3,1);
text1 = ['text1';'text1';'text1']
text2 = ['text2';'text2';'text2']
TABLE.rows = {'1';'2';'3'};
TABLE.cols = {'num1';'num2';'num3';'num4';'text1';'text2'};
T = table(num1,num2,num3, num4,text1,text2,...
'VariableNames',TABLE.cols,'RowNames',TABLE.rows);
TABLE.AB = uitable('Parent', fig, 'Units', 'normal', 'Position', [0 1/2 1/3 1/2],...
'Data', T{:,:},'ColumnName',T.Properties.VariableNames,...
'RowName',T.Properties.RowNames);

Risposte (1)

Guillaume
Guillaume il 11 Nov 2018
Note: since R2016b, string is a proper matlab type, delimited by double quotes ".
What you have are char arrays. In particular, your text1 and text2 variables are 2D char arrays. Don't use 2D char arrays, they're very impractical. For example, your code would have failed if any of the three lines of text had a different length from the others:
text1 = ['text1'; 'text99'; 'text1']; %ERROR: dimensions of arrays being concatenated are not consistent
Always use a cell array of char vectors instead:
text1 = {'text1'; 'text99'; 'text1'}; %Will work regardless of the length of each string
which will solve your uitable error

Categorie

Prodotti

Release

R2014a

Tag

Richiesto:

il 11 Nov 2018

Risposto:

il 11 Nov 2018

Community Treasure Hunt

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

Start Hunting!

Translated by