Building an array of strings that might be empty

42 次查看(过去 30 天)
Ken
Ken 2024-4-10,15:05
编辑: Stephen23 2024-4-10,18:07
I want to make an array of four elements, containing the contents of four edit fields, which might potentially be empty. First try:
app.graphLimits = [app.XminEditField.Value, app.XmaxEditField.Value, ...
app.YminEditField.Value, app.YmaxEditField.Value]
But no! This syntax appears to concatenate the strings, and if the fields are all empty, the result is precisely one empty string.
Second try:
app.graphLimits(4) = app.YmaxEditField.Value;
app.graphLimits(1) = app.XminEditField.Value;
app.graphLimits(2) = app.XmaxEditField.Value;
app.graphLimits(3) = app.YminEditField.Value;
But no! It complains with an error message that I really don't understand
Unable to perform assignment because the indices on the left side are not compatible with the size of the right side.
What? I'm trying to initialize and pre-allocate an array of four elements, the fourth of which is (in this case) an empty string.
What am I missing? What is the correct syntax to accomplish what I'm trying to accomplish?

回答(3 个)

Fangjun Jiang
Fangjun Jiang 2024-4-10,15:15
That is because app.XminEditField.Value and etc are char array, not string
use string()
s=["a","","b","c"]
s = 1x4 string array
"a" "" "b" "c"
c=['a','','b','ded']
c = 'abded'
c=[string('a'),string(''),string('b'),string('ded')]
c = 1x4 string array
"a" "" "b" "ded"
  1 个评论
Fangjun Jiang
Fangjun Jiang 2024-4-10,16:26
Or a little easier this way. But have to put in whitespace ' ', not ''
c=['a';' ';'b';'d']
c = 4x1 char array
'a' ' ' 'b' 'd'
s=string(c)
s = 4x1 string array
"a" " " "b" "d"

请先登录,再进行评论。


the cyclist
the cyclist 2024-4-10,15:18
If the fields are empty strings, your first syntax will not concatenate them. It will make them into a string array:
app.XminEditField.Value = "";
app.XmaxEditField.Value = "";
app.YminEditField.Value = "";
app.YmaxEditField.Value = "";
app.graphLimits = [app.XminEditField.Value, app.XmaxEditField.Value, ...
app.YminEditField.Value, app.YmaxEditField.Value]
app = struct with fields:
XminEditField: [1x1 struct] XmaxEditField: [1x1 struct] YminEditField: [1x1 struct] YmaxEditField: [1x1 struct] graphLimits: ["" "" "" ""]
Are you possibly using character arrays and not strings? (I'm guessing yes.)
Can you upload a small example of the data you are working with?

Stephen23
Stephen23 2024-4-10,18:01
编辑:Stephen23 2024-4-10,18:04
"What am I missing?"
The differences between strings and characters. Do not mix up string arrays with character vectors (which is what you probably have), they are completely different things with very different properties:
"What is the correct syntax to accomplish what I'm trying to accomplish?"
Use a cell array (rather than concatenating all of the character vectors together like you tried):
app.graphLimits = {app.XminEditField.Value, app.XmaxEditField.Value, ...
app.YminEditField.Value, app.YmaxEditField.Value};
  1 个评论
Stephen23
Stephen23 2024-4-10,18:06
编辑:Stephen23 2024-4-10,18:07
"I'm trying to initialize and pre-allocate an array of four elements"
The code you show does not preallocate anything. But the error is very easy to reproduce:
a(1) = 'hello'
Unable to perform assignment because the indices on the left side are not compatible with the size of the right side.
The reason is very simple: you are trying to force multiple characters into one element of a character array. One element of a character array holds exactly one character, thus what you are attempting will not work.

请先登录,再进行评论。

类别

Help CenterFile Exchange 中查找有关 Characters and Strings 的更多信息

产品


版本

R2023b

Community Treasure Hunt

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

Start Hunting!

Translated by