필터 지우기
필터 지우기

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

조회 수: 12 (최근 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 CenterFile Exchange에서 Cell Arrays에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by