How to use a value of type 'cell' as an index.
13 ビュー (過去 30 日間)
古いコメントを表示
I have a Cell letters = A, B, C, D, E, F, G....![Unbenannt.PNG](https://www.mathworks.com/matlabcentral/answers/uploaded_files/205071/image.png)
![Unbenannt.PNG](https://www.mathworks.com/matlabcentral/answers/uploaded_files/205071/image.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
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
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
0 件のコメント
参考
カテゴリ
Help Center および File Exchange で Matrices and Arrays についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!