Clear Filters
Clear Filters

My uitable cells do not hold text input?

7 views (last 30 days)
Alex
Alex on 29 Feb 2024
Edited: Alex on 1 Mar 2024
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
  7 Comments
Alex
Alex on 29 Feb 2024
One other detail that I think may affect it:
I have this functioning correctly in one table where I use the set() function with an matrix.
The table that does not function correctly -- the table cells I want to edit are initialized using set() with a cell array. The cells I want to edit are {'-'} in that cell array.
Alex
Alex on 29 Feb 2024
My app is attached, it's my first app so somewhat unfamiliar with some conventions.
To replicate the problem:
  1. Go to Device Setup Tab
  2. Type something into "Device Tag"
  3. Click "add device"
  4. Go to Sequence Development tab
  5. Click "draw shapes"
  6. Draw out an ROI on the figure with your cursor
  7. The table should add a row below -- you can also add rows that will exhibit the same behavior using the button at the bottom.
The Step No. is not editable, the Step Description will not save text input from the GUI, and the column for the tag you named should open to a dropdown when clicked.

Sign in to comment.

Accepted Answer

Voss
Voss on 29 Feb 2024
Edited: Voss on 29 Feb 2024
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 Comments
Voss
Voss on 29 Feb 2024
Edited: Voss on 29 Feb 2024
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.
Alex
Alex on 1 Mar 2024
Edited: Alex on 1 Mar 2024
Thank you for taking an extra look at this!
1. This line is actually supposed to add 1 or more column to the table. It sets initial values to the cells for each new tag added. It adds as many tags as are not currently shown. There is some opportunity to simplify by removing the repmat and num2cell, you are right:
data(:,end+1:(length(tags)+2)) = {'-'};
2. I will take a look at the places I can simplify by initializing the table. I did that with the app.ROI_array and it helped avoid some complexity.
3. Unfortunately I think for my application I will always have to reformat columns on the fly because I modify the dropdown menu options for the addition and deletion of tags. If I delete a tag, the column may need to go from dropdowns of "ON/OFF" to dropdowns of "OPEN/CLOSE".

Sign in to comment.

More Answers (0)

Categories

Find more on Develop Apps Using App Designer in Help Center and File Exchange

Products


Release

R2023b

Community Treasure Hunt

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

Start Hunting!