Creating an array of string variables
2 次查看(过去 30 天)
显示 更早的评论
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 个评论
Rick Rosson
2011-8-16
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.
采纳的回答
Rick Rosson
2011-8-16
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
更多回答(0 个)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Loops and Conditional Statements 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!