Building an array of strings that might be empty
3 views (last 30 days)
Show older comments
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?
0 Comments
Answers (3)
Fangjun Jiang
on 10 Apr 2024
That is because app.XminEditField.Value and etc are char array, not string
use string()
s=["a","","b","c"]
c=['a','','b','ded']
c=[string('a'),string(''),string('b'),string('ded')]
1 Comment
Fangjun Jiang
on 10 Apr 2024
Or a little easier this way. But have to put in whitespace ' ', not ''
c=['a';' ';'b';'d']
s=string(c)
the cyclist
on 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]
Can you upload a small example of the data you are working with?
0 Comments
Stephen23
on 10 Apr 2024
Edited: Stephen23
on 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 Comment
Stephen23
on 10 Apr 2024
Edited: Stephen23
on 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'
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.
See Also
Categories
Find more on Characters and Strings in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!