필터 지우기
필터 지우기

Show coordinates for a 2D point in a table cell

조회 수: 29 (최근 30일)
Quist
Quist 2024년 7월 6일 7:20
편집: Voss 2024년 7월 11일 15:38
I'm working on a MATLAB app where I'm trying to dynamically record and show every mouse click on the UIAxes in a UItable.
My problem is that the table only shows "1x2 double" in the designated cell for the coordinates instead of the actual numbers.
Is this possible to fix or do I have to split them into two columns?
app.PointsTable.Data = table('Size',[0 2],'VariableNames', {'Point', 'Coordinates'}, 'VariableTypes', {'uint8', 'cell'});
function UIAxesButtonDown(app, event)
if 1
coordinates = app.UIAxes.CurrentPoint(1, 1:2);
numRows = size(app.PointsTable.Data, 1);
app.PointsTable.Data(numRows+1, :) = {numRows+1, {[coordinates(1) coordinates(2)]}};
end
end

채택된 답변

Voss
Voss 2024년 7월 7일 23:20
One way to display a vector in a single cell of a uitable is to make it into a string:
app.PointsTable.Data = table( ...
'Size', [0 2], ...
'VariableNames', {'Point', 'Coordinates'}, ...
'VariableTypes', {'uint8', 'string'}); % string type now, not cell
function UIAxesButtonDown(app, event)
if 1
coordinates = app.UIAxes.CurrentPoint([1 2]);
numRows = size(app.PointsTable.Data, 1);
app.PointsTable.Data(numRows+1, :) = {numRows+1, "[" + join(compose("%f",coordinates),",") + "]"}
end
end
  댓글 수: 2
Quist
Quist 2024년 7월 11일 5:52
Thanks! I had planned to also use the table as storage location of the data for further processing later. So then I have to do the reverse operation (text to vector), or perhaps it’s better to mirror the data also in a matrix, just using the table for UI purposes…
Voss
Voss 2024년 7월 11일 15:34
편집: Voss 2024년 7월 11일 15:38
You're welcome!
Regarding the question of using the uitable for storage vs mirroring it, in my opinion:
  1. if the user can edit the uitable (add/delete rows, modify cell contents), then use the uitable as the storage with no variable(s) mirroring its contents
  2. if the user cannot edit the uitable, then use the variable(s) on which the uitable's contents are based when you need to access the data; if there are no such variables, then you'd need to implement them, or just use the uitable as storage (option 1)
In case you need it, here's an example showing one way to recover the numeric data from the strings in the uitable:
% a table variable:
app.PointsTable.Data = table( ...
'Size', [0 2], ...
'VariableNames', {'Point', 'Coordinates'}, ...
'VariableTypes', {'uint8', 'string'});
% add five rows of random coordinates:
for ii = 1:5
coordinates = rand(1,2);
numRows = size(app.PointsTable.Data, 1);
app.PointsTable.Data(numRows+1, :) = {numRows+1, "[" + join(compose("%f",coordinates),",") + "]"};
end
% show the table variable:
disp(app.PointsTable.Data)
Point Coordinates _____ _____________________ 1 "[0.415212,0.448916]" 2 "[0.409778,0.733798]" 3 "[0.663416,0.919541]" 4 "[0.610271,0.833671]" 5 "[0.075813,0.838912]"
% get a numeric matrix of the coordinates:
F = @(s)str2num(s,'Evaluation','restricted');
C = arrayfun(F,app.PointsTable.Data.Coordinates,'UniformOutput',false);
M = vertcat(C{:});
% show the numeric matrix
format long g
disp(M)
0.415212 0.448916 0.409778 0.733798 0.663416 0.919541 0.610271 0.833671 0.075813 0.838912

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

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Develop Apps Using App Designer에 대해 자세히 알아보기

태그

제품


릴리스

R2024a

Community Treasure Hunt

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

Start Hunting!

Translated by