Search code examples
matlabuser-interfacematlab-uitable

Setting and getting with uitable


I'm creating a Nx3 uitable since it seems much easier than multiple text-edits when N is high. I initialised the columns as cell array in order to initially obtain an 'empty' table.

hinitial = cell(N,1);
hfinal = cell(N,1);
hporosity = cell(N,1);
DataInput = [hinitial;hfinal;hporosity];
ColumnName = {'n_Initial','n_Final','Porosity'};
ColumnFormat = {'numeric', 'numeric', 'numeric'};
ColumnEditable = [false true false];

htable = uitable(fig,'Units','pixels','Position',[20 20 260 204],...
         'Data', DataInput,...
         'ColumnName', ColumnName,...
         'ColumnEditable', ColumnEditable);  

1) With this pushbutton I want to loop-print the data into the first column.

function uploadbutton_Callback(source,eventdata) 
S = load('n.mat');
for K = 1:N
set(htable(K,1),'Data',num2cell(S.n(K)));
end
end

But this does the printing only for the first row of that column. Then this error is displayed.

Index exceeds matrix dimensions.
Error in bandprovaprog/uploadbutton_Callback (line 122)
set(htable(K,1),'Data',num2cell(S.n(K)));
Error while evaluating UIControl Callback.

2) In the second column I want to manually input data, then save the results in a mat file using a pushbutton. My effort so far is this:

function donebutton_Callback(source,eventdata) 

m = zeros(1,N);
 for J = 1:N
 m(J) = str2double(get(hfinal{J},'String'));  
 end
save('m.mat','m');
end

3) Last column again uses set to print in the third column so I think I can do it once I know how to do 1)

EDIT2: edit of donebutton_Callback

function donebutton_Callback(source,eventdata) 

 m = zeros(1,N);
 m(1:N) = str2double(htable.Data(1:N,2),'Data'); 
 save('m.mat','m');
end

I want to put the content of the second column into m.mat


Solution

  • 1) Your set command seems dodgy, setting the Data like that will attempt to set all of the data I think. Instead simply assign to the specific Data element

    function uploadbutton_Callback(source,eventdata) 
        S = load('n.mat');
        for K = 1:N
            htable.Data{K,1} = S.n(K);
        end 
    end
    

    Better still, vectorise your code and avoid looping

    function uploadbutton_Callback(source,eventdata) 
        S = load('n.mat');
        htable.Data(1:N,1) = num2cell(S.n(1:N));
    end
    

    Similarly for (2), don't use get, simply access the data as shown above.


    Edit:

    Your new problem is the following. You are creating 3 Nx1 cell arrays, then concatenating them to pass as data to your table. When trying to edit column 2 of your data, you can't because you get the following error

    Warning: Table data is not editable at this location.

    This is because (as suggested in this answer) you are not providing enough data to populate all columns of your table. This is confusing at first, until you look in your workspace...

    workspace

    You are expecting that variable to be Nx3, but instead it is 3Nx1! You need to use commas to concatenate rows, instead of semi-colons to concatenate columns when forming DataInput:

    DataInput = [hinitial, hfinal, hporosity];
    % Equivalently to remove ambiguity, you could use: 
    % DataInput = horzcat(hinitial, hfinal, hporosity)
    

    Once you update this, the table behaves as expected.