2

I'm new with MATLAB-GUI. I watch a couple of video and I understand how check box are working (the base) but it seems that you have to pre-defined where and how many check box you will have.

I have a table or structure in MATLAB (import from CSV with manipulation)

Ex of first fews columns:

Date      | Ticker | ShortName                     | RedCode
08-Jun-16 | NWS    | 21st Century Fox America, Inc.| 9J143F
08-Jun-16 | III    | 3i Group Plc                  | GOGCBA

That I want to 'import' in a GUI (scrolling box with all the rows with a checkbox for each row at the right end) so the user will choose the rows he want to use (check the checkbox).

Then when the user has choose all the rows he want in his database, I want to import/export them back to MATLAB (using the GUI as a filter, where the user choose manually the names he want), button import.

What do I need to do to import the select rows with a #checkbox to the right, considering that the number of rows will be different from one time to another and export them back to MATLAB to use that list?

Petter Friberg
  • 21,252
  • 9
  • 60
  • 109
MC_B
  • 31
  • 4
  • 2
    See: [Table Containing Mixed Data Types](https://www.mathworks.com/help/matlab/ref/uitable.html#bvdu5y5-1) – sco1 Aug 11 '17 at 13:51

2 Answers2

3

As described in the documentation linked by @excaza, you can do this by creating a uitable and catching the handle:

f = figure;
t = uitable(f);

And then adding the data (in cell array format) to t.data. Explore the properties of t for more things you can set up programatically! (you can do this by opening the variable "t" in the workspace, double click)

Ander Biguri
  • 35,140
  • 11
  • 74
  • 120
3

The documentation for uitable provides an example that serves as a great starting point. You can then utilize tools like logical indexing to address various properties of your uitable object in order to get the desired table output.

For example:

function testgui
% Set up some data
LastName = {'Smith';'Johnson';'Williams';'Jones';'Brown'};
Age = [38;43;38;40;49];
Height = [71;69;64;67;64];
Weight = [176;163;131;133;119];
tf = false(size(LastName));
T = table(Age, Height, Weight, tf);

% Build a GUI
f = figure('Name', 'A uitable', 'NumberTitle', 'off', 'MenuBar', 'none', 'ToolBar', 'none');
uit = uitable('Parent', f, 'Data', table2cell(T), ...
              'Units', 'Normalized', 'Position', [0.1, 0.15, 0.8, 0.8], ...              
              'RowName', LastName, 'ColumnName', {'Age', 'Height', 'Weight', 'Export?'}, ...
              'ColumnEditable', [false false false true]);
butt = uicontrol('Parent', f, 'Style', 'pushbutton', 'String', 'Export Data', ...
                 'Units', 'Normalized', 'Position', [0.1, 0.05, 0.8 0.1], ...
                 'Callback', @(h,e)table2workspace(uit));
end

function table2workspace(uit)
tmp = uit.Data(:, 4);  % Get the state of our checkboxes
exportbool = [tmp{:}];  % Denest the logicals from the cell array
outT = cell2table(uit.Data(exportbool, 1:3), 'VariableNames', uit.ColumnName(1:3), ...
                  'RowNames', uit.RowName(exportbool));
assignin('base', 'outT', outT);  % Dump to base MATLAB workspace for demo purposes
end

Which gives us a demo GUI that we can use to output tables of various shapes to the base MATLAB workspace:

yay

sco1
  • 12,154
  • 5
  • 26
  • 48