From: Jess on 20 Nov 2006 17:21 So I'm trying to fill a variable, "numstars" with user input: prompt = {'How many stars would you like to plot?'}; dlg_title = 'Please enter number of stars...'; num_lines = 1; def = {'all'}; [num] = inputdlg(prompt,dlg_title,num_lines,def); if (num{1}) == 'all', numstars = 300 else numstars = str2num(num{1}), end ....And this block of code works fine for user input = 'all' or '1','2',...,'9'. It even works fine for 3-digit numbers, but when I try to input a two-digit number I get this error message: ??? Error using ==> eq Matrix dimensions must agree. Error in ==> starmap at 51 if (num{1}) == 'all', numstars = 300 I don't understand why inputdlg doesn't like 2-digit numbers...<sigh>...
From: PB on 20 Nov 2006 17:26 On 2006-11-20 23:21 Jess said the following: > So I'm trying to fill a variable, "numstars" with user input: > > prompt = {'How many stars would you like to plot?'}; > dlg_title = 'Please enter number of stars...'; > num_lines = 1; > def = {'all'}; > > [num] = inputdlg(prompt,dlg_title,num_lines,def); > > if (num{1}) == 'all', numstars = 300 > else numstars = str2num(num{1}), end > > ...And this block of code works fine for user input = 'all' > or '1','2',...,'9'. It even works fine for 3-digit numbers, but when > I try to input a two-digit number I get this error message: > > ??? Error using ==> eq > Matrix dimensions must agree. > > Error in ==> starmap at 51 > if (num{1}) == 'all', numstars = 300 > > I don't understand why inputdlg doesn't like 2-digit > numbers...<sigh>... Use strcmp when comparing strings. /PB -- "Never attribute to malice that which can be adequately explained by stupidity."
From: Steven Lord on 21 Nov 2006 14:24 "Jess" <j.a.lord.stuff(a)gmail.com> wrote in message news:ef466d5.-1(a)webcrossing.raydaftYaTP... > So I'm trying to fill a variable, "numstars" with user input: > > prompt = {'How many stars would you like to plot?'}; > dlg_title = 'Please enter number of stars...'; > num_lines = 1; > def = {'all'}; > > [num] = inputdlg(prompt,dlg_title,num_lines,def); > > if (num{1}) == 'all', numstars = 300 > else numstars = str2num(num{1}), end > > ...And this block of code works fine for user input = 'all' > or '1','2',...,'9'. It even works fine for 3-digit numbers, but when > I try to input a two-digit number I get this error message: > > ??? Error using ==> eq > Matrix dimensions must agree. > > Error in ==> starmap at 51 > if (num{1}) == 'all', numstars = 300 > > I don't understand why inputdlg doesn't like 2-digit > numbers...<sigh>... INPUTDLG is not what's causing the error. Your use of == is what's causing the error. The num output from INPUTDLG is a cell array containing a char array -- a string. The size of the char array in num{1} is 1-by-n, where n is the number of characters in the string. When you enter 'all' in the prompt, num{1} is a 3-element char array: num{1} = 'all' When you write [num{1] == 'all'], == compares each pair of characters in turn. Is the first character of num{1} equal to 'a'? It is. How about the second character, is it an 'l'? Yes. The third character matches too, so what you've written collapses to: if [true true true], numstars = 300; ... If all the elements of the condition of an IF statement are true, the IF portion of the statement executes. If at least one is false, the IF portion of the statement doesn't execute. In the case where you enter '1' or '2' in inputdlg, MATLAB uses what's known as "scalar expansion" when it hits the == operator. Basically it says "since there's only one element in the first argument, I'll expand that scalar to be the same size as the second input and then compare corresponding elements." Therefoe, it compares '1' with 'a', '1' with 'l', and '1' with 'l'. All three of those comparisons are false, so we go to the ELSE part of the statement. In the case where you enter '102', MATLAB compares '1' with 'a', '0' with 'l', and '2' with 'l'. That's why the 3-digit number worked. In the case where you enter '10', MATLAB can't perform scalar expansion, and the two expressions you're passing to == don't have the same number of elements, so it correctly gives you the error "matrix dimensions must agree". Instead of using ==, use the ISEQUAL function. If the two inputs to ISEQUAL are not the same size, it returns false (because they can't be equal) rather than throwing an error. -- Steve Lord slord(a)mathworks.com
|
Pages: 1 Prev: R2006a problem Next: Invalid settings for input port dimension of |