Filter löschen
Filter löschen

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

12 Ansichten (letzte 30 Tage)
Rakesh Yadav Kodari
Rakesh Yadav Kodari am 20 Feb. 2019
Beantwortet: Steven Lord am 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 Kommentar
Stephen23
Stephen23 am 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.

Melden Sie sich an, um zu kommentieren.

Antworten (1)

Steven Lord
Steven Lord am 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.

Kategorien

Mehr zu Cell Arrays finden Sie in Help Center und File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by