From: Matteo on 15 May 2010 23:29 I am trying to create a character array containing names for some filters using this code: for i=1:10 names(i,:)=(['filter' char(num2str(i))]) end I get the result below and also an error message. Can someone help me understand what is happening I do not see why the mismatch? Also, is there an easier way to create the character array as I want it? Thank you so much nms = filter1 filter2 filter3 filter4 filter5 filter6 filter7 filter8 filter9 ??? Subscripted assignment dimension mismatch.
From: ImageAnalyst on 15 May 2010 23:32 filter10 doesn't have the same number of characters as the prior 9 strings and so it bombs. You'd need a cell array to handle different sized strings. for i=1:10 names{i,:}=(['filter' char(num2str(i))]) end An alternate way to create the strings would be to use sprintf() - I always prefer that way over the bracket way because it's usually more compact and flexible/general. for i=1:10 names{i,:} = sprintf('filter%d', i) end
From: Matteo on 15 May 2010 23:36 *** Sorry, got the wrong variable names in my original post *** I am trying to create a character array containing names for some filters using this code: for i=1:10 names(i,:)=(['filter' char(num2str(i))]) end I get the result below and also an error message. Can someone help me understand what is happening I do not see why the mismatch? Also, is there an easier way to create the character array as I want it? Thank you so much names = filter1 filter2 filter3 filter4 filter5 filter6 filter7 filter8 filter9 ??? Subscripted assignment dimension mismatch.
From: Matteo on 15 May 2010 23:37 Awesome - thank you! I have been struggling with these kind of issues forever. ImageAnalyst <imageanalyst(a)mailinator.com> wrote in message <0c8500d9-2763-45df-926f-ae2073024adf(a)v18g2000vbc.googlegroups.com>... > filter10 doesn't have the same number of characters as the prior 9 > strings and so it bombs. You'd need a cell array to handle different > sized strings. > > for i=1:10 > names{i,:}=(['filter' char(num2str(i))]) > end > > An alternate way to create the strings would be to use sprintf() - I > always prefer that way over the bracket way because it's usually more > compact and flexible/general. > > for i=1:10 > names{i,:} = sprintf('filter%d', i) > end
From: James Tursa on 16 May 2010 02:24 "Matteo " <matteo.niccoli(a)gmail.com> wrote in message <hsnoq1$da8$1(a)fred.mathworks.com>... > I am trying to create a character array containing names for some filters using this code: > > for i=1:10 > names(i,:)=(['filter' char(num2str(i))]) > end > > I get the result below and also an error message. Can someone help me understand what is happening I do not see why the mismatch? Also, is there an easier way to create the character array as I want it? Thank you so much > nms = > > filter1 > filter2 > filter3 > filter4 > filter5 > filter6 > filter7 > filter8 > filter9 > > ??? Subscripted assignment dimension mismatch. For the first 9 iterations num2str(i) returns a 1 character string, so the size of the columns of names is the same. But for the 10th iteration num2str(i) returns a 2 character string, so the result of ['filter' char(num2str(i))] will not fit in the names array with its current size. Try pre-allocating the names array so that all of the rows will fit, and then change your assignment to accommodate different sized right hand sides. James Tursa
|
Next
|
Last
Pages: 1 2 Prev: plotting an implicit 3d function Next: serial communication with 8051 controller |