Azzera filtri
Azzera filtri

How to use a value of type 'cell' as an index.

12 visualizzazioni (ultimi 30 giorni)
Rakesh Yadav Kodari
Rakesh Yadav Kodari il 20 Feb 2019
Risposto: Steven Lord il 20 Feb 2019
I have a Cell letters = A, B, C, D, E, F, G....Unbenannt.PNG
I need to use it for indexing for example
frames(letters)=fscanf(port);
I expected framesA= fscanf(port);
framesB= fscanf(port);
framesC= fscanf(port);
framesD= fscanf(port); an so on
but the error message is "Unable to use a value of type 'cell' as an index."
Could anyone help me
  1 Commento
Stephen23
Stephen23 il 20 Feb 2019
Your examples do not show indexing.
Your examples show that you are trying to dynamically change the variable names.
Dynamically changing variable names is one way that beginners force themselves into writing slow, complex, obfuscated, buggy code that is hard to debug. Read this to know why:
Indexing is neat, simple, and very efficient (unlike what you are trying to do).
Indexing uses positive integers only.

Accedi per commentare.

Risposte (1)

Steven Lord
Steven Lord il 20 Feb 2019
For this particular example, using that cell array of names as the VariableNames or RowNames of a table may be appropriate.
names = {'A', 'B', 'C'};
Create a sample table from the columns of a magic matrix.
M = magic(3);
T = array2table(M, 'VariableNames', names)
You can obtain a smaller table from the larger table T using parentheses.
subtable = T(2, 'C')
You can obtain the value stored in the table using curly braces or by extracting a smaller table and using dot indexing.
value = T{2, 'C'}
valueAnotherWay = T(2, :).C
See this documentation page for more information on the ways to access data in a table.

Categorie

Scopri di più su Cell Arrays in Help Center e File Exchange

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by