Adding a string value to an array.

조회 수: 30 (최근 30일)
Louis Grainger
Louis Grainger 2021년 4월 21일
답변: Aghamarsh Varanasi 2021년 4월 23일
Given an array of zeros like so....
0 0 0 0
0 0 0 0
0 0 0 0
Using something like
if something = true
A(2,2) = ("x");
end
Is there a function which will use the str(x) and transform the element in the array at (2,2) to this string?
  댓글 수: 1
Stephen23
Stephen23 2021년 4월 21일
Not really. Arrays are homogenous, the entire array consists of elements of the same class.
As an alternative you could do one of these:
  • use a container array (e.g. cell array, but this will make processing numeric data more complex and less efficient)
  • use a special value (e.g. NaN, -1)
  • store the character value (but this might be easily confused with valid numeric data)
  • store a separate logical mask and/or string/char array.

댓글을 달려면 로그인하십시오.

채택된 답변

Aghamarsh Varanasi
Aghamarsh Varanasi 2021년 4월 23일
Hi,
CellArrays would be the right structure to store string data. You can assign values to cell array as follows.
A = cell(4,4);
A{2,2} = 'x';
Hope this helps

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Characters and Strings에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by