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.
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 件のコメント

David Goodmanson
David Goodmanson 2019 年 6 月 24 日
Hi Justin
a = "etaoinshrdlu"
class(a)
ans = 'string'
size(a)
ans = 1 1
a(5:6) % produces an error
b = 'etaoinshrdlu'
class(b)
ans = 'char'
size(b)
ans = 1 12
b(5:6)
ans = 'in'
so character arrays behave like you might expect, but strings are an entirely different animal. Strings are useful for some purposes, no doubt..
Walter Roberson
Walter Roberson 2019 年 6 月 24 日
Scalar in computer programs usually means "exactly one item". In MATLAB scalar numbers are also array that are 1x1. In many programming languages, arrays and scalars are considered to be different data types. For example in C if you have
A = 42;
Then that is a scalar value, and you cannot use A[0] (array indexing starts at 0 in C)
In MATLAB, a scalar string object holds exactly one string, which is a complete vector of characters.
Jay
Jay 2019 年 6 月 24 日
Thank you David and Walter for further explaining what a Scalar String means.
Nzuzo Thabiso
Nzuzo Thabiso 2025 年 6 月 5 日
great insight Walter
Nzuzo Thabiso
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
dpb 2019 年 6 月 24 日
移動済み: Cris LaPierre 2025 年 6 月 5 日

0 投票

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 件のコメント

Jay
Jay 2019 年 6 月 24 日
移動済み: Cris LaPierre 2025 年 6 月 5 日
Thank you dpb, that is the information I required.
Stephen23
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
Steven Lord 2025 年 6 月 5 日

2 投票

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_42 = 1×2
1 1
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>
size_pi = size(pi)
size_pi = 1×2
1 1
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>
size_1_2 = size([1 2])
size_1_2 = 1×2
1 2
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>
size_square_brackets = size([])
size_square_brackets = 1×2
0 0
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>
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([])]
scalarTest = 1×4 logical array
1 1 0 0
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)
numberOfStrings = 1×2
1 1
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>
lengthOfEachString = strlength(s)
lengthOfEachString = 8
isStringScalar(s) % true since numberOfStrings = [1 1]
ans = logical
1
veggies = ["asparagus", "zucchini"];
numberOfStrings = size(veggies)
numberOfStrings = 1×2
1 2
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>
lengthOfEachString = strlength(veggies)
lengthOfEachString = 1×2
9 8
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>
isStringScalar(veggies) % false since numberOfStrings is [1 2]
ans = logical
0

カテゴリ

質問済み:

Jay
2019 年 6 月 24 日

回答済み:

2025 年 6 月 5 日

Community Treasure Hunt

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

Start Hunting!

Translated by