My uitable cells do not hold text input?
조회 수: 6 (최근 30일)
이전 댓글 표시
I have a uitable object in an app. The first column of the table is not editable -- I cannot click or type anything in it. The second column should be editable with free text input. I can click and type letters(ex. "blahblasdfh"), but when I press "enter" or click away, it just reverts to its default value (ex. a simple dash "-").
I control the format of the columns of this table, and suspect this may be the root of the problem. The column I need to be able to edit has format 'char' currently. As I understand it, there is no way to format a table column for strings input, but it is functionally the same as a 'char' vector for me.
Why doesn't the table hold my text once I press enter? Any tips?
function refreshColumnFormat(app)
tag = get(app.SequenceTable, 'ColumnName');
actuation_type = app.TagTable.Data(:,3);
formatArray = cell([1,length(tag)]);
for i = 1:(length(tag))
if i<=2
formatArray{i} = {'char'};
else
if ismember(actuation_type(i-2),'OPEN/CLOSE')
formatArray{i} = {'OPEN', 'CLOSE'};
elseif ismember (actuation_type(i-2), 'ON/OFF')
formatArray{i} = {'ON', 'OFF'};
elseif ismember(actuation_type(i-2), 'RUN/STOP')
formatArray{i} = {'RUN', 'STOP'};
end
end
end
app.SequenceTable.ColumnFormat = formatArray;
end
채택된 답변
Voss
2024년 2월 29일
편집: Voss
2024년 2월 29일
OK, I see what the problem is now (you were right, it is a problem with the refreshColumnFormat function).
This line in refreshColumnFormat
formatArray{i} = {'char'};
makes the content of the ith cell of formatArray a scalar cell array (the 1x1 cell array {'char'}). This means that the ith column will have dropdowns that have one item in them ('char'). (Except the dropdown arrow doesn't appear when there's only one item. Try typing the word "char" in that table cell and you'll see it is accepted and doesn't revert to the dash ("-").)
Compare to other lines where you are setting formatArray{i}, e.g.:
formatArray{i} = {'OPEN', 'CLOSE'};
That makes the content of the ith cell of formatArray a 1x2 cell array, as intended. The only difference between that and the previous is one is a scalar cell array and one is a 1x2 cell array.
What you really meant to do is not to put a cell array in the ith cell of formatArray at all, but rather put the character vector 'char' in there.
To do that, use
formatArray{i} = 'char';
or
formatArray(i) = {'char'};
either of which makes the content of the ith cell of formatArray the character vector 'char', which makes that table column accept char inputs.
댓글 수: 3
Voss
2024년 2월 29일
편집: Voss
2024년 2월 29일
You're welcome!
I noticed a few other things while I was looking around in the code:
1. Probable bug in DRAWSHAPESButtonPushed, on this line:
data = [data, num2cell(repmat('-',1,size(data,1)))];
I believe the intent there is to add a new row to data, but if that's the case it should be:
data = [data; num2cell(repmat('-',1,size(data,2)))];
% ^ vertical concatenation ^ replicate to the number of columns, not rows
And FYI, you can avoid num2cell, and just repmat a scalar cell array to do the same thing:
data = [data; repmat({'-'},1,size(data,2))];
or even more simply:
data(end+1,:) = {'-'};
2. Rather than having SequenceTable.Data be [] (i.e., empty numeric array) initially, you can inititalize it to a cell array with the correct number of columns (and zero rows), e.g., in your startupFcn:
app.SequenceTable.Data = cell(0,2);
This makes it possible to avoid having to check isempty(app.SequenceTable.Data) in numerous places and doing something different depending on whether it's empty or not. For instance, adding a new row of data is easy:
app.SequenceTable.Data(end+1,:) = {'-'}; % add a new row of '-' (or whatever you want the new row of data to be)
That works no matter how many rows Data has - including zero - because Data always has the correct number of columns.
Obviously the same can be done for any table (e.g., app.TagTable).
3. Similar to #2, it's a good idea to initialize all the column properties of any table (i.e., ColumnName, ColumnFormat, ColumnEditable, etc.) to their correct initial values, either by typing stuff into the component browser in Design View or by writing code in the startupFcn. (In the case of SequenceTable, it initially has 2 columns, so all those properties would be of length 2.)
That way, they are all consistent with each other and consistent with the size of the table's Data. Then as the code operates, you might be adding columns to or removing columns from a table, but you don't generally have to rebuild any table properties from scratch, the way refreshColumnFormat does now. You just have to keep them all consistent (and consistent with Data), which means whenever you add or remove a column, you add or remove an element from all those column properties (and add a column or remove a column from Data) at the same time.
추가 답변 (0개)
참고 항목
카테고리
Help Center 및 File Exchange에서 Migrate GUIDE Apps에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!