How Do You Correctly Concatenate Strings in a MATLAB Loop?

  • Context: MATLAB 
  • Thread starter Thread starter Saladsamurai
  • Start date Start date
  • Tags Tags
    Matlab
Click For Summary

Discussion Overview

The discussion revolves around how to correctly concatenate strings in a MATLAB loop, particularly in the context of user input and variable assignment. Participants explore various syntax options and approaches for handling string concatenation and variable naming within loops.

Discussion Character

  • Technical explanation
  • Conceptual clarification
  • Debate/contested
  • Mathematical reasoning

Main Points Raised

  • One participant seeks help on how to concatenate strings in a loop using MATLAB syntax, expressing confusion over the use of '&' for concatenation.
  • Another participant provides a solution using square brackets and the function num2str to concatenate strings, which is acknowledged positively by the original poster.
  • A participant asks for clarification on the necessity of square brackets in the provided syntax for string concatenation.
  • It is noted that num2str is required to convert numeric values to strings for concatenation purposes.
  • A new question arises about assigning values to variables named x1, x2, etc., suggesting a desire for dynamic variable naming within a loop.
  • One participant warns against assigning a number to a string and suggests using array indexing instead, highlighting the advantages of MATLAB's matrix functionality.
  • Another participant proposes an alternative syntax using a 'do' loop, but this is met with skepticism regarding its effectiveness for the intended variable assignments.

Areas of Agreement / Disagreement

Participants express differing views on the best approach to variable assignment and string concatenation in MATLAB. While some agree on the utility of using arrays, others continue to explore the idea of dynamic variable naming, indicating that the discussion remains unresolved.

Contextual Notes

There are limitations regarding the assumptions about variable naming conventions and the implications of using certain MATLAB functions, which are not fully explored in the discussion.

Saladsamurai
Messages
3,009
Reaction score
7
This is so easy I am sure...but I have no idea how to look it up.

I want to execute a loop from i = 1 to N

During each iteration I want to prompt the user for the i-th input. Let's say, I want to ask for

x1, x2, x3, ..., xN.

I should be able to write something like:

Code:
for i:N
    x(i) = input(' Enter value of x' & i)
next

But the '&' does not seem to be the proper way to do this...

Anyone know this one?

Thanks! :smile:
 
Physics news on Phys.org
for i = 1:3
x(i) = input([' Enter value of x' num2str(i) ': ']);
end
 
Wow! You're my hero right now matonski!

Now maybe I can get a little extra out of you? I am used to using

Code:
For i = 1:3
x(i) = input(' stuff ')
end

Can you tell me litle about your syntax? Why are the '[' brackets now required? as in your syntax

Code:
for i = 1:3
x(i) = input([' Enter value of x' num2str(i) ': ']);
end

Thanks again!
 
Since i is a number, I had to use num2str to convert it to a string. Then, the brackets are used to concatenate the separate strings.
 
matonski said:
Since i is a number, I had to use num2str to convert it to a string. Then, the brackets are used to concatenate the separate strings.

Excellent! So brackets are used to concatenate strings. Perfect!

Thanks again :smile:
 
What about this one?

I want to assign the value 5 to the variables x1,x2,x3 so I would like to be able to do something to the effect of

Code:
for i = 1:3

[x num2str(i)] = 5

Thanks!

EDIT: I just tried

strcat(x, num2str(i))= 5

and Horrible things happened! Haha...
 
Horrible things happened because you are trying to assign a number to a string. It's like trying to assign 5 to the number 4.

I suggest not naming variables that way and simply using x(1), x(2), etc. There's a way using the function eval, but that's generally frowned upon. Besides, using arrays let's you make use of all matlab's matrix functionality very simply and straightforwardly.

For example, if you wanted to just set x(1) through x(3) to 5, you could just do x(1:3) = 5
 
Alternatively, you could do this:
Code:
do i=1:3
  x(i) = 5
end
This is longer than the way matonski suggested, but it's more in line with many other programming languages.
 
Mark44 said:
Alternatively, you could do this:
Code:
do i=1:3
  x(i) = 5
end
This is longer than the way matonski suggested, but it's more in line with many other programming languages.

Hmmm. I don't think this will do. This will set the i-th element of the array (vector) x to 5 for each value of i. Whereas I want to set one variable named 'x1' equal to 5 and 'x2' =5, etc

Edit: ok Mark, I see you were simply complementing matonski's post and thus are implying that I should use an array :smile:
 

Similar threads

  • · Replies 5 ·
Replies
5
Views
4K
  • · Replies 4 ·
Replies
4
Views
3K
  • · Replies 1 ·
Replies
1
Views
3K
  • · Replies 1 ·
Replies
1
Views
2K
  • · Replies 14 ·
Replies
14
Views
4K
  • · Replies 3 ·
Replies
3
Views
2K
  • · Replies 4 ·
Replies
4
Views
2K
  • · Replies 1 ·
Replies
1
Views
3K
  • · Replies 5 ·
Replies
5
Views
2K
  • · Replies 4 ·
Replies
4
Views
2K