How to find the variable by cell array contents
4 visualizaciones (últimos 30 días)
Mostrar comentarios más antiguos
Hi, I have a cell array that looks like this:
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/147289/image.jpeg)
It's (2,10) element is also a cell array that looks like this:
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/147290/image.jpeg)
As you can see, in both these two cell arrays, I have column and row headings. I now need to give the values to the elements in the cell array MyCell (2,10) from other variables. I have got those variable names pretty corresponding to the headings, such as Cargo_age_bulk_freq = [1:9];
What I really want to do is give the value of Cargo_age_bulk_freq to the row starting with Bulk (in the blue circle) in myCell{2,10} which is Cargo -- Age freq in myCell. Please help me with finding a way to do this in a less tediously manner!
Thanks a lot!
0 comentarios
Respuestas (1)
Brendan Hamm
el 24 de Feb. de 2015
Editada: Brendan Hamm
el 24 de Feb. de 2015
Assuming that Cargo_age_bulk_freq is a numeric row vector of length 9, then we can do the following
>> myCell{2,10}(2,2:end) = num2cell(Cargo_age_bulk_freq);
Likely you were trying something like:
>> myCell{2,10}{2,2:end} = Cargo_age_bulk_freq;
Error: The right hand side of this assignment has too few values to satisfy the left hand side.
The reason for this is due to comma separated lists, that is the indexing: >> myCell{2,10}{2,2:end}
will print each value of the (inner-most) cell array you are indexing into separately. This is called a comma separated list. To clarify, it is extracting the content type of the inner-most cell array one at a time as if printed in a for-loop, implying that the attempted assignment of your vector would be to one element of the left hand side cell. To avoid this we assign to the block of the cell array myCell{2,10}(2,2:end), and therefore the right hand side of the assignment must be cell array of the same size. In this case the the size is 1-by-9.
2 comentarios
Brendan Hamm
el 25 de Feb. de 2015
Sorry for the misinterpretation,
If this is the objective I would highly suggest having the names of the columns and rows which define the variable name to be in the same case and order the variable is named.
Suppose this were the case we have the variables:
% Extract the names and make them all column vectors for simplicity
myCellColName = myCell(1,:)'; % Cell array of the column names (as col vector)
myCellRowName = myCell(:,1); % Cell array of the row names
ageFreqName = myCell{2,10}(:,1); % Cell array of row names
% Concatenate the strings into the variable name
varname = [myCellRowName{2},'_',myCellColName{10},'_',agefreqname{2}];
% also note it is cleaner/easier to not use underscores in the variable name
eval(varname)
%You will notice this is exactly the data contained in the variable with the same name as the character array varname
This should give you the pieces you need to put this together.
Ver también
Categorías
Más información sobre Matrix Indexing en Help Center y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!