What Is A Scalar String?
조회 수: 56 (최근 30일)
이전 댓글 표시
Can someone please tell me what is meant by "scalar string object"?
I am finding it difficult to understand how a string can be a scalar (to me, scalars represent a multiplier?).
I have searched the MathWorks Documentation (Define String Scalar Inputs [states how to define a string scalar but not what one is]) and still can not wrap my head around what is meant.
I have also searched MATLAB Answers (SWITCH expression must be a scalar or string constant. , Why do I receive an error about a SWITCH expression when using ODE15S within MATLAB? & String [converting a "tokenized" document]).
Does the "Scalar String" defintion mean single elements comprising of multiple strings, for example:
A (1,1) = "Todays Date Is June 24th, 2019" as a string
A (1,1) = "Todays" "Date" "Is" "June" "24th," "2019" as a scalar string object
or am I completely off the mark?
댓글 수: 5
Nzuzo Thabiso
2025년 6월 5일
Hi walter please assist with ways of rectifying this error it is appearing on the BEAR Toolbox for rconometrics
채택된 답변
dpb
2019년 6월 24일
이동: Cris LaPierre
2025년 6월 5일
A scalar string is simply a string that is not an array of strings...see https://www.mathworks.com/help/fixedpoint/ug/code-generation-for-strings.html beginning discussion.
댓글 수: 2
Stephen23
2025년 6월 5일
"A scalar string is simply a string that is not an array of strings"
A scalar string is an array, it is an array with size 1x1(x1x1x...). The linked documentation also states "A 1-by-1 string array, called a string scalar,..." (bold added). Because in MATLAB everything is an array:
In other words, there is no string that is not an array.
The defining characteristic of a scalar string (or a scalar anything, for that matter), is that it has size 1x1(x1x1...). That is all.
추가 답변 (1개)
Steven Lord
2025년 6월 5일
A scalar array in MATLAB is one whose size is [1 1]. So for example 42 and pi are scalars, while [1 2] and [] are not.
size_42 = size(42)
size_pi = size(pi)
size_1_2 = size([1 2])
size_square_brackets = size([])
One way to check if an array is a scalar array is using the isscalar function. Based on the sizes above we'd expect the following command to return [true true false false] and it does.
scalarTest = [isscalar(42), isscalar(pi), isscalar([1 2]), isscalar([])]
For text data, there's an additional tool you can use, the isStringScalar function. Here's a string array that has size [1 1] (there's only one string in the array.) Note that the length of the text stored in that string is not relevant to answering the question "is the string array a scalar string", just the size of the string array itself is relevant. To obtain the length of each string in the string array you'd want to use the strlength function.
s = "alphabet";
numberOfStrings = size(s)
lengthOfEachString = strlength(s)
isStringScalar(s) % true since numberOfStrings = [1 1]
veggies = ["asparagus", "zucchini"];
numberOfStrings = size(veggies)
lengthOfEachString = strlength(veggies)
isStringScalar(veggies) % false since numberOfStrings is [1 2]
댓글 수: 0
참고 항목
카테고리
Help Center 및 File Exchange에서 String에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!