Building an array of strings that might be empty

4 visualizzazioni (ultimi 30 giorni)
Ken
Ken il 10 Apr 2024
Modificato: Stephen23 il 10 Apr 2024
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?

Risposte (3)

Fangjun Jiang
Fangjun Jiang il 10 Apr 2024
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 Commento
Fangjun Jiang
Fangjun Jiang il 10 Apr 2024
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"

Accedi per commentare.


the cyclist
the cyclist il 10 Apr 2024
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 il 10 Apr 2024
Modificato: Stephen23 il 10 Apr 2024
"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 Commento
Stephen23
Stephen23 il 10 Apr 2024
Modificato: Stephen23 il 10 Apr 2024
"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.

Accedi per commentare.

Categorie

Scopri di più su Characters and Strings in Help Center e File Exchange

Prodotti


Release

R2023b

Community Treasure Hunt

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

Start Hunting!

Translated by