如何将表的值传递给矩阵

时间:2019-04-02 17:29:14

标签: matlab user-interface linear-algebra

我正在尝试在matlab中创建一个GUI,该GUI接受表中的值以将其转换为矩阵,但其想法是用户可以首先设置行数和列数。
面板看起来像这样 enter image description here
并且按钮的代码是

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))

但是,如何导出或转换为矩阵,以便可以对其应用函数?

更新 在byetisener的帮助下,我将代码更新为 函数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

但这给出了一个空矩阵
enter image description here

不是要获取单元格的值,而是假设按钮会同时调整大小并复制值,如果矩阵大小调整后如何在另一个按钮中复制呢?

2 个答案:

答案 0 :(得分:1)

我不确定是否能回答您的问题,但是您可以采用这种方法。

首先,如果您有兴趣,在MATLAB中使用点表示法比使用setter和getter方法更快。

所以,您可以做的是:

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

,当然,或者:

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

如果您想要将可使用的数据转换为矩阵,则可以使用:

matrix = cell2mat(handles.uitable1.Data);

如果表中包含非数字值:

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

希望这会有所帮助。让我知道您是否解决了问题。

答案 1 :(得分:1)

您的代码存在一些问题:

  1. 您实际上并没有在其中分配值,只是将uitable的Data设置为一个空单元格数组。
num_elem =

  1×2 cell array

    {0×0 char}    {0×0 char}
  1. 如果成功,您的代码会将所需的所有内容仅写入uitable的第一列。因为您没有遍历行。该按钮仅添加到第一行。
  2. 如果表中的数据类型不同,
  3. cell2mat()函数将不起作用。您可能会认为您没有不同的数据类型,但是空单元格是单元格类型,而您输入的数据是double类型,所以就存在。

为解决所有这些问题,我为您重写了一个回调函数。您可以直接将此代码粘贴到您的回调中,以替换您的代码。我应该在最后提供您想要的矩阵,它在我的计算机中也可以。

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

只需检查所有变量名称是否相同,并且不要忘记接受is作为答案。希望能帮助到你。