0
votes

Im trying to do a GUI in matlab that accepts the values in a table to converting it to a matrix, but the idea is that a user can set the number of rows and columns first.
The panel looks like this enter image description here
and the code for the push button is

function pushbutton1_Callback(hObject, eventdata, handles)
% hObject    handle to pushbutton1 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
rows =str2double(get(handles.edit_rows,'String'));
cols=str2double(get(handles.edit_cols,'String'));
num_elem=cell(rows,cols);
num_elem(:,:)={"};
set(handles.uitable1,'Data',num_elem)
set(handles.uitable1,'ColumnEditable',true(1,cols))

But then, how can export or convert to a matrix so I can apply functions to it?

UPDATE With the help of byetisener I updated the code to function pushbutton1_Callback(hObject, eventdata, handles)

% hObject    handle to pushbutton1 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
filas=str2double(get(handles.edit_fila,'String'));
column=str2double(get(handles.edit_col,'String'));
num_elem=cell(filas,column);
num_elem(:,:)={''};
set(handles.uitable1,'Data',num_elem)
set(handles.uitable1,'ColumnEditable',true(1,column))
handles.uitable1.Data = cell(filas, column);
matrix = cell2mat(handles.uitable1.Data);
matrix

but this is giving an empty matrix
enter image description here

It is not taking the values of the cells, it is supposed that the button resizes and copy the values at the same time, if not how con copy in another button once the matrix is resized?

2
You are wanting to convert cell values to a number correct?medicine_man
Yes, thats right!riccs_0x
Cells can contain multiple data types. Just access the cell, and convert it to the type you want. Based on your code, your cell content is already a double.medicine_man

2 Answers

1
votes

There are some problems about your code:

  1. You do not really assign values here, you are just setting the Data of the uitable to an array of empty cells.
num_elem =

  1×2 cell array

    {0×0 char}    {0×0 char}
  1. If you ever succeed, you code will write everything you want to only the first column of the uitable. Because you are not iterating through rows. The pushbutton only adds to the first row.
  2. cell2mat() function won't work if you have different data types in your table. You may think that you do not have different data types, but empty cells are type cell and the data you enter is type double, so there it is.

To solve all of this I have rewritten a callback function for you. You may directly paste this code to your callback, replacing yours. I should give you the matrix you want at the end, it does in my computer.

filas  = str2double(handles.edit_fila.String);
column = str2double(handles.edit_col.String);

% This loop looks for an empty row to write new data
for i = 1:length(handles.uitable1.Data) 
   if isempty(handles.uitable1.Data{i,1})
       handles.uitable1.Data(i,1) = {filas};
       handles.uitable1.Data(i,2) = {column};
       break;
   else
       disp('Error occured');
   end
end

% This double for loop check if there are any empty cells 
% if it finds one, it changes it to 0, so all the cells have the same type
for i = 1:length(handles.uitable1.Data) 
    for j = 1:2                         
        if isempty(handles.uitable1.Data{i,j})
            handles.uitable1.Data(i,j) = {0};
        else
            disp('Error occured');
        end
    end
end

matrix = cell2mat(handles.uitable1.Data); % The matrix you want

Just check if all the variable names are the same and do not forget to accept is as an answer. Hope it helps.

1
votes

I am not sure if this answers you question but you can follow this approach.

First of all, if you are interested, using dot notation is faster in MATLAB than setter and getter methods.

So, what you can do is:

handles.uitable1.Data = cell(rows, cols);

or, of course, alternatively:

set(handles.uitable1, 'Data', cell(rows,cols));

If what you want to is to convert the data in uitable to a matrix, you can use:

matrix = cell2mat(handles.uitable1.Data);

If you table contains non-numeric values:

tableData = handles.uitable1.Data;
tableData = [str2double(tableData(:, 1)), cell2mat(tableData(:, 2))];

Hope this helps. Let me know if you solve your problem.