Home How to assign a matrix to a cell of a cell array
Reply: 1

How to assign a matrix to a cell of a cell array

Yujian
1#
Yujian Published in 2018-01-12 16:07:26Z

I'm working on matlab and try to assign a matrix to one cell of a cell array. However, there was always something wrong. Here is the code:

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

Then matlab prompted that

"Expected one output from a curly brace or dot indexing expression, but there were 12 results."

But if I don't use 'ID' and 'Info' but use '1' and '2' instead, the matrix could be assigned successfully.

Could anyone help me? Thanks!

Hossein Ka
2#
Hossein Ka Reply to 2018-01-12 20:39:43Z

Assuming we have got three persons and each one has got a name and ID number and the data size that corresponds to each person is 2x3. I utilize a cell for storing data and fill it via random number.(In your case you should use xlsread('myexcelfile') to fill this cell). Each ID number is concatenated with a string because Matlab does not accept a string which is directly converted by number, for names in rows and columns of the table.

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
% 

After creating our table we can get input as follows:

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

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

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

And if the input formats are adapted to the table, we can get output like this:

  table

             ID48    
         ____________

Brown    [2×3 double]

You can add some conditions to the script for the cases that inputs are not adapted to the table format.

You need to login account before you can post.

About| Privacy statement| Terms of Service| Advertising| Contact us| Help| Sitemap|
Processed in 0.310243 second(s) , Gzip On .

© 2016 Powered by mzan.com design MATCHINFO