如何将矩阵分配给单元阵列的单元格

时间:2018-01-12 16:07:26

标签: matlab matrix cell-array

我正在使用matlab并尝试将矩阵分配给单元阵列的一个单元格。然而,总有一些错误。这是代码:

    C = {};
    myMatrix = xlsread('myexcelfile');
    C{'ID', 'info'} = myMatrix;

然后matlab提示

  

"预期来自大括号或点索引表达式的一个输出,但有12个结果。"

但如果我不使用ID'和'信息'但使用' 1'和' 2'相反,矩阵可以成功分配。

有人能帮帮我吗?谢谢!

1 个答案:

答案 0 :(得分:0)

假设我们有三个人,每个人都有一个姓名和身份证号码,每个人对应的数据大小为2x3。我利用一个单元格来存储数据并通过随机数填充它。(在你的情况下,你应该使用xlsread('myexcelfile')来填充这个单元格)。每个ID号都用一个字符串连接起来,因为Matlab不接受一个直接由数字转换的字符串,用于表格的行和列中的名称。

clc;clear all;close all;
%  assuming we have got three persons in the dataset 
cell_data=cell(3,3); % I use cell instead of matrix for storing data
ID_number=[38;48;58];% this vector contains the ID numbers of each person

for i=1:numel(ID_number);rng('shuffle');cell_data{i,i}=rand(2,3);end % using  random number as dataset 

ID=strcat('ID ',string(ID_number));%'38' is not a valid variable name so concat number with 'ID ' string 
Customer = string({'Jones';'Brown';'Smith'});
Customer = cellstr(Customer);
T = table('RowNames',Customer); 
for i=1:numel(ID_number)
T.(char(ID(i)))=cell_data(:,i);
end
% 

创建表后,我们可以按如下方式获取输入:

input_cell = inputdlg({'Name','ID number'});% 2x1 cell

ID_input=strcat('ID ',input_cell{2,1});

T( {input_cell{1,1}} , {ID_input} )

enter image description here

如果输入格式适应表格,我们可以得到如下输出:

  table

             ID48    
         ____________

Brown    [2×3 double]

对于输入未适应表格格式的情况,您可以为脚本添加一些条件。

相关问题