Creating an array of string variables
2 vues (au cours des 30 derniers jours)
Afficher commentaires plus anciens
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 commentaire
Rick Rosson
le 16 Août 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.
Réponse acceptée
Rick Rosson
le 16 Août 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
Plus de réponses (0)
Voir également
Catégories
En savoir plus sur Loops and Conditional Statements dans Help Center et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!