Domanda

I've got a GUI in MATLAB which uses uitables for input. There are a fixed number columns, and each column has a very specific format which I have stored as a cell array, like so:

columnformat = {'text', 'numeric', {@doSomething, inputArg1}, {'Option1' 'Option2'}};

The number of rows is theoretically unlimited; the user could provide as many they like. The back-end is capable of handling arbitrarily many row inputs. Right now, I'm building a large uitable initially, and just assuming the user won't use it all.

Here's the question: I want to set up the table and associated code such that any time the user has selected the final row and presses enter, it creates a new row with the same format as the rest of the table.

I've tried many different approaches, including dynamically setting 'Data', and they all seem to break the custom formatting dictated by the cell array. I'm sure someone has done this before. Thanks for your help!

È stato utile?

Soluzione 2

I couldn't think of a possibility to achieve what you want with a certain key, I think it would be possible with any key (KeyPressFcn). But I'd rather recommend to introduce a toolbar with a pushbutton:

h = figure(...
u = uitable(h, ...
set(u,'Tag','myTable')
tbar = uitoolbar(h);
uipushtool(tbar,'ClickedCallback',@addRow);

In your callback function then you need to get your data, add a row and write it back:

function addRow(~,~)

u = findobj(0,'Type','uitable','Tag','myTable');
data = get(u,'Data');
%// modify your data, add a row ...
set(src,'Data',data);

end

Sorry if everything is a little simple and untested, but a good answer would require a considerable effort, I don't have time for. The tag can give you a lot of further ideas.

Altri suggerimenti

This solution works on GUI created using MATLAB GUIDE. I think it's true that MATLAB GUI shows odd behaviour, but I have seen most of the odd behaviour when debugging MATLAB callbacks using something like keyboard and not properly exiting from them using dbquit. So, my advice would be to stay away from using keyboard related commands for MATLAB GUIs created with GUIDE.

Now, back to solving your problem, follow these steps.

Step 1: Add this at the start of GUINAME__OpeningFcn:

handles.row_col_prev = [1 1];

Step 2: Click on the properties of the table in context and click on CellSelectionCallback. Thus, if the tag of the table is uitable1, it would create a function named - uitable1_CellSelectionCallback.

Assuming the figure of the GUI has the tag - addrows_figure

Add these in it:

%%// Detect the current key pressed
currentkey = get(handles.addrows_figure,'CurrentCharacter')

%%// Read in previous row-col combination
prev1 = handles.row_col_prev

%%// Read in current data. We need just the size of it though.
data1 = get(handles.uitable1,'Data');

%%// Main processing where a row is appended if return is pressed
if numel(prev1)~=0
    if size(data1,1)==prev1(1) & currentkey==13 %%// currentkey==13 denotes carriage return in ascii
        data1(end+1,:) = repmat({''},1,size(data1,2)); %%// Append empty row at the end 
        set(handles.uitable1,'Data',data1); %%// Save it back to GUI
    end
end

%%// Save the current row-col combination for comparison in the next stage
%%// when selected cell changes because of pressing return
handles.row_col_prev = eventdata.Indices;
guidata(hObject, handles);

Hope this works out for you!

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top