Building an array of strings that might be empty (2024)

13 views (last 30 days)

Show older comments

Ken on 10 Apr 2024

  • Link

    Direct link to this question

    https://webchat.mathworks.com/matlabcentral/answers/2105236-building-an-array-of-strings-that-might-be-empty

  • Link

    Direct link to this question

    https://webchat.mathworks.com/matlabcentral/answers/2105236-building-an-array-of-strings-that-might-be-empty

Edited: Stephen23 on 10 Apr 2024

Open in MATLAB Online

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

Show -2 older commentsHide -2 older comments

Sign in to comment.

Sign in to answer this question.

Answers (3)

Fangjun Jiang on 10 Apr 2024

  • Link

    Direct link to this answer

    https://webchat.mathworks.com/matlabcentral/answers/2105236-building-an-array-of-strings-that-might-be-empty#answer_1439441

  • Link

    Direct link to this answer

    https://webchat.mathworks.com/matlabcentral/answers/2105236-building-an-array-of-strings-that-might-be-empty#answer_1439441

Open in MATLAB Online

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 Comment

Show -1 older commentsHide -1 older comments

Fangjun Jiang on 10 Apr 2024

Direct link to this comment

https://webchat.mathworks.com/matlabcentral/answers/2105236-building-an-array-of-strings-that-might-be-empty#comment_3127521

  • Link

    Direct link to this comment

    https://webchat.mathworks.com/matlabcentral/answers/2105236-building-an-array-of-strings-that-might-be-empty#comment_3127521

Open in MATLAB Online

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"

Sign in to comment.

the cyclist on 10 Apr 2024

  • Link

    Direct link to this answer

    https://webchat.mathworks.com/matlabcentral/answers/2105236-building-an-array-of-strings-that-might-be-empty#answer_1439446

  • Link

    Direct link to this answer

    https://webchat.mathworks.com/matlabcentral/answers/2105236-building-an-array-of-strings-that-might-be-empty#answer_1439446

Open in MATLAB Online

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?

0 Comments

Show -2 older commentsHide -2 older comments

Sign in to comment.

Stephen23 on 10 Apr 2024

  • Link

    Direct link to this answer

    https://webchat.mathworks.com/matlabcentral/answers/2105236-building-an-array-of-strings-that-might-be-empty#answer_1439496

  • Link

    Direct link to this answer

    https://webchat.mathworks.com/matlabcentral/answers/2105236-building-an-array-of-strings-that-might-be-empty#answer_1439496

Edited: Stephen23 on 10 Apr 2024

Open in MATLAB Online

"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:

https://www.mathworks.com/help/matlab/characters-and-strings.html

"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

Show -1 older commentsHide -1 older comments

Stephen23 on 10 Apr 2024

Direct link to this comment

https://webchat.mathworks.com/matlabcentral/answers/2105236-building-an-array-of-strings-that-might-be-empty#comment_3127606

  • Link

    Direct link to this comment

    https://webchat.mathworks.com/matlabcentral/answers/2105236-building-an-array-of-strings-that-might-be-empty#comment_3127606

Edited: Stephen23 on 10 Apr 2024

Open in MATLAB Online

"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.

Sign in to comment.

Sign in to answer this question.

See Also

Categories

MATLABLanguage FundamentalsData TypesCharacters and Strings

Find more on Characters and Strings in Help Center and File Exchange

Tags

  • array
  • string
  • empty

Products

  • MATLAB

Release

R2023b

Community Treasure Hunt

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

Start Hunting!

An Error Occurred

Unable to complete the action because of changes made to the page. Reload the page to see its updated state.


Building an array of strings that might be empty (7)

Select a Web Site

Choose a web site to get translated content where available and see local events and offers. Based on your location, we recommend that you select: .

You can also select a web site from the following list

Americas

  • América Latina (Español)
  • Canada (English)
  • United States (English)

Europe

  • Belgium (English)
  • Denmark (English)
  • Deutschland (Deutsch)
  • España (Español)
  • Finland (English)
  • France (Français)
  • Ireland (English)
  • Italia (Italiano)
  • Luxembourg (English)
  • Netherlands (English)
  • Norway (English)
  • Österreich (Deutsch)
  • Portugal (English)
  • Sweden (English)
  • Switzerland
    • Deutsch
    • English
    • Français
  • United Kingdom(English)

Asia Pacific

Contact your local office

Building an array of strings that might be empty (2024)
Top Articles
Latest Posts
Article information

Author: Pres. Lawanda Wiegand

Last Updated:

Views: 5880

Rating: 4 / 5 (71 voted)

Reviews: 94% of readers found this page helpful

Author information

Name: Pres. Lawanda Wiegand

Birthday: 1993-01-10

Address: Suite 391 6963 Ullrich Shore, Bellefort, WI 01350-7893

Phone: +6806610432415

Job: Dynamic Manufacturing Assistant

Hobby: amateur radio, Taekwondo, Wood carving, Parkour, Skateboarding, Running, Rafting

Introduction: My name is Pres. Lawanda Wiegand, I am a inquisitive, helpful, glamorous, cheerful, open, clever, innocent person who loves writing and wants to share my knowledge and understanding with you.