Clear Filters
Clear Filters

Creating an array of string variables

2 views (last 30 days)
Hello,
I am trying to create an array that holds a bunch of strings. My idea was to be able to index into the array in a for loop and access the string that is stored in that particular index. I am trying to do this but I keep getting a "Subscripted assiment dimension mismatch" error and I know what I want MATLAB to do, I just do not knw the syntax well enough to make it work. Here is a sample code of what I am trying...
name = 'DevilsWash_Multi_Sage_004_'; s_name = ['surface_' name ]; p = ones(9,30); for n = 1:length(p(:,1)) p(n)=[s_name int2str(n)]; end
As you can see, I am adding a number to each string to each is unique but I do not want to have each string be a variable in my workspace, I need them to be contained in an array. Any suggestions?
ps, I do not know how to enter code in MATLAB forums yet, sorry for any inconvenience there.
Thank you for your time and effort, Luke
  1 Comment
Rick Rosson
Rick Rosson on 16 Aug 2011
To enter code, simply indent each line of code with two or three blank space characters. For regular non-code prose, you should NOT indent at all.

Sign in to comment.

Accepted Answer

Rick Rosson
Rick Rosson on 16 Aug 2011
Please try creating a "cell array" of strings:
myList = { 'Red' ; 'Orange' ; 'Yellow' ; 'Green' ; 'Blue' } ;
for k = 1:5
disp(myList{k});
end
Notice that cell arrays use curly braces instead of standard parentheses.
HTH.
Rick

More Answers (0)

Categories

Find more on Loops and Conditional Statements 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!