필터 지우기
필터 지우기

Uitable data converison for user precision

조회 수: 16 (최근 30일)
Jason
Jason 2019년 2월 24일
댓글: Jason 2019년 2월 24일
Hello, I have a uitable with double values:
data =
374.35 406.81 383.00 336.46 297.88
436.10 362.88 303.35 245.88 199.66
189.35 200.21 218.86 223.13 204.46
341.85 322.08 288.71 239.71 203.11
I am wanting to display these in the uitable with only 1 decimal place (they are currently displayed as 4 decimal places)
I found the following on this site:
%Convert to user precision
data = get(t, 'data')
class(data)
for i = 1:numel(data)
% if(isnumeric(data{i}))
tempStr = sprintf('%2.4g', data{i});
data{i} = tempStr;
% end
end
set(t, 'data', data);
However I get the error "Cell contents referenced from a non-cell array object" in this line tempStr = sprintf('%2.4g', data{i});

채택된 답변

Stephen23
Stephen23 2019년 2월 24일
편집: Stephen23 2019년 2월 24일
Your data is numeric, not a cell array.
You already answered your own question when you wrote "I have a uitable with double values..:"
You originally created the UITABLE with numeric data, so its data will still be numeric when you access it later. It will not change into a cell array.
"I found the following on this site: "
That code applies to a UITABLE that was created using a cell array, so its data will still be a cell array when it is accessed later. It appears that you got that code from this thread, where data is clearly defined as a cell array:
There are two ways to control the format of numeric data shown in a UITABLE:
  1. Create the UITABLE using numeric data, setting the ColumnFormat option, which supports similar options as format does (long, short, bank, etc).
  2. Create a cell array of character vectors from the numeric data and use that cell array to create the UITABLE.
Only the second option lets you specify an arbitrary number of decimal digits and total control over the formatting.
  댓글 수: 3
Stephen23
Stephen23 2019년 2월 24일
@Jason: why not just use repmat?:
C = repmat({'yourFormatChoice'},1,numberOfColumns);
uitable(...., 'ColumnFormat',C)
Jason
Jason 2019년 2월 24일
Perfect, thankyou
Jason

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Migrate GUIDE Apps에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by