MATLAB Answers

Birch
0

Use uitable with static text?

Asked by Birch
on 29 Aug 2017
Latest activity Commented on by JB
on 30 Aug 2017
Please help. I am preparing a GUI (guide) and have a couple of questions:
1) from a pushbutton I want to import the filename of all .txt files in the directory and place the filename in a uitable as static text.
2) in the uitable I want only one row to start with, but add one new empty row at the end to add new info manually. This should be a uigetfile, potentially through a pushbutton, to allow the user to include other .txt files located outside the dir. Every time the last row is filled it should automatically add a new empty row to the uitable. And finally:
3) import all the .txt (mixed data and char) files specified in the uitable potentially via a loop to generate mixed data arrays.
I know this is a lot but hopefully someone can help me. I am open to better suggestions if any. THANKS a lot.

  0 Comments

Sign in to comment.

Products

1 Answer

Answer by Walter Roberson
on 29 Aug 2017

uitable cannot have static text exactly. You can set a cell to a string and set the associated ColumnEditable to false, but that affects all entries in the column which might be a problem as you also want users to be able add new entries.
You can can set the celledit callback to set the contents back if they are changed.
You will need one of those callback functions to detect that the user has entered something so that you would know to add a new row.

  3 Comments

Birch
on 29 Aug 2017
Thanks for your comment. I don't want the user to type in anything but to be able to use uigetfile to include files located outside the dir. I rephrased 2) for clarity.
function push1_callback(hObject, event, handles)
dname = uigetdir('', 'Select a directory');
if ~ischar(dname); return; end %user canceled
dinfo = dir( fullfile( dname, '*.txt') );
filenames = fullfile( dname, {dinfo.name} );
set( handles.uitable1, 'Data', filenames(:) );
function push2_callback(hObject, event, handles)
[filename, dname] = uigetfile('*.txt', 'Select additional files');
if ~ischar(filename); return; end %user canceled
filename = fullfile(dname, filename);
existing_data = get( handles.uitable1, 'data');
existing_data{end+1,1} = filename;
set( handles.uitable1, 'Data', existing_data );
function push3_callback(hObject, event, handles)
existing_data = get( handles.uitable1, 'data');
mask = cellfun(@isempty, existing_data);
existing_data(mask, :) = [];
numfile = size(existing_data, 1);
filecontents = cell(numfile, 1);
for K = 1 : numfile
this_file = existing_data{K, 1};
filecontents{K} = import_one_file( this_file );
end
handles.filecontents = filecontents;
guidata(hObject, handles); %save the contents in the handles structure
... for appropriate import_one_file routine that you will need to write
JB
on 30 Aug 2017
AWESOME Walter, thanks a lot.

Sign in to comment.