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 Exchange에서 Large 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