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);

回答 (1 件)

Guillaume
Guillaume 2018 年 11 月 11 日

0 投票

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

カテゴリ

ヘルプ センター および File ExchangeLarge Files and Big Data についてさらに検索

製品

リリース

R2014a

タグ

質問済み:

2018 年 11 月 11 日

回答済み:

2018 年 11 月 11 日

Community Treasure Hunt

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

Start Hunting!

Translated by