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

15 ビュー (過去 30 日間)
Rakesh Yadav Kodari
Rakesh Yadav Kodari 2019 年 2 月 20 日
回答済み: Steven Lord 2019 年 2 月 20 日
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 件のコメント
Stephen23
Stephen23 2019 年 2 月 20 日
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.

サインインしてコメントする。

回答 (1 件)

Steven Lord
Steven Lord 2019 年 2 月 20 日
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.

カテゴリ

Help Center および File ExchangeCell Arrays についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by