1
votes

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!

1
So why don't you use C{1,2} = myMatrix? (or whichever numbers you need instead of 1, 2) What result do you want to achieve? - Luis Mendo
@LuisMendo I want to obtain an array that whenever I input the users' names and IDs, I can get the corresponding information, which is stored in a matrix. That's why I have to use strings instead of numbers. - Yujian
If you want to index by names you probably need a table, not a cell array. Cell arrays use numerical or logical indexing. (They can be indexed with characters, but that doesn't give the result you want). See some examples of indexing into tables here - Luis Mendo
@LuisMendo Alright. Thank you. Maybe I should try another way. - Yujian

1 Answers

0
votes

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

enter image description here

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.