2017-08-11 36 views
2

我是MATLAB-GUI的新手。我看了幾個video,我理解複選框是如何工作的(基礎),但似乎你必須預先定義你將擁有的複選框的位置和數量。如何使用每行的複選框將表/結構導入GUI,然後將選擇列表導出到MATLAB?

我在MATLAB表或結構(進口從CSV手法)

防爆第一饑荒預警系統列:

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

,我想在一個GUI「進口」(滾動框所有行右端的每行都有一個複選框),因此用戶將選擇他想使用的行(選中複選框)。

然後,當用戶選擇他想要在他的數據庫中的所有行,我想導入/導出他們回到MATLAB(使用GUI作爲過濾器,用戶手動選擇他想要的名稱),按鈕導入。

我需要做什麼才能將選擇行導入到右邊的#checkbox中,考慮到行數會因時間而不同,並將它們導出到MATLAB以使用該列表?

+2

參見:表含混合數據類型](https://www.mathworks.com/help/matlab/ref/uitable.html#bvdu5y5-1) – excaza

回答

3

As described in the documentation通過@excaza鏈接,你可以通過創建一個uitable和追趕手柄做到這一點:

f = figure; 
t = uitable(f); 

,然後將數據(在單元陣列格式)增加t.data。探索t的屬性,以獲得更多可以以編程方式設置的內容! (可以通過在工作區中打開變量「t」來完成此操作,雙擊)

3

uitable的文檔提供了an example作爲一個很好的起點。然後,您可以使用諸如logical indexing之類的工具來尋址各種properties of your uitable object以獲得所需的表格輸出。

例如:

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 

這給了我們,我們可以用各種形狀的產出表的基礎MATLAB工作空間演示GUI:

yay

+0

這個答案比我的好得多! –

+0

非常感謝!對不起,只是意識到我沒有按上次發送的評論。這裏真的很新鮮! :) –

+0

@MC_B如果其中一個答案幫助您解決了您的問題,請考慮點擊答案左側的綠色複選標記。 – excaza

相關問題