0
votes

I'm trying to create a GUI in Matlab that plots a specific graph once you've entered your data in the edit text zones. My problem is, when I try to plot a graph using this code:

  function pushbutton3_Callback(hObject, eventdata, handles)
  a = str2num(get(handles.edit1,'string'));
  b = str2num(get(handles.edit2,'string'));
  n1 = str2num(get(handles.edit6,'string'));
  n2 = str2num(get(handles.edit4,'string'));
  lambda = str2num(get(handles.edit5,'string'));
  m = ones(1,a)
  s = ones(1,b)
  f = ones(1,n1)
  g = ones(1,n2)
  k = ones(1,lambda) 
  c = k.*(m.*s/f.*g)
  i = -3:1:5 
  figure
  p= plot(c,i) 

This error message keeps poping out: Error times Matrix dimensions must agree. I don't know what to do? I seriously need help because I have a deadline for this project and I'm clueless! Thanks in advance.

1
please use some more linebreaks and format your code by appending four spaces in front AND include (at least) one empty line before your code. Can you also provide some example data, for example, what's the value of a,b,n1,n2 and lambdaGunther Struyf
they are variables that the user of the gui will enter and I have to plot my graph using those values. I tried to plot them without converting them to vectors but I only get points on the figure and I needed a graph not points !Ben Tahar Khouloud
ah, here is the info! What do you mean with 'my graph'? a set of datavalues stored in a matrix/vector/... or maybe a function you have to evaluate at the points the user specifies.. What's a for? b? n1? the x-limits of the window? the points to evaluate? Can you tell what the user has to input (for each variable separately) and how that fits into your 'I want to plot my graph using those values'?Gunther Struyf
the equation I want to plot is : c=lambda*(a*b/n1*n2) ! in the gui I have multiple edit text zones where the user inputs differents values which are lambda,a,b,n1,n2 ! the problem is when I used string values directly to plot I only got different points on the figure but I've been told I have to convert those string values into vectors of ones in order to have a graph ! but as I posted that error message keeps poping out and I couln't plot a thing and I really need this project by this thursday ! so what do u think ?Ben Tahar Khouloud

1 Answers

1
votes
m = ones(1,a)
s = ones(1,b)
f = ones(1,n1)
g = ones(1,n2)
k = ones(1,lambda) 
c = k.*(m.*s/f.*g)

didn't your forget a dot at the division operator?

c = k.*(m.*s./f.*g)

And for the error itself: unless a==b==n1==n2==lambda, the dimensions of the vectors you create are not the same and you will not be able to (element-wisely) multiply them, which causes the Error times Matrix dimensions must agree

EDIT

plot(x,y) draws a line connecting the points (x,y) to eachother. If you drop the x and only use plot(y) then x is assumed to be 1:N (N being the number of elements in the vector y)

If you want to plot the value c=lambda*(a*b/n1*n2), then plot(c) will just plot that value. I don't understand why someone told you to convert it to vectors using ones??

Just to be sure, if

lambda=2
a  = 3
b  = 4
n1 = 5
n2 = 6

then c=2*(3*4/5*6)=28.8 What you then want is to plot the value 28.8??

If this is what you want, then this is done using

c = k.*(m.*s./f.*g);
plot(c);

If the parameters contain more values, eg:

lambda= [2 4]
a  = [3 5]
b  = [4 8]
n1 = [5 4]
n2 = [6 1]

the same thing applies, but c = k.*(m.*s./f.*g); will return c=[28.8 40] and plot(c) will connect the points (1; 28.8) and (2; 40)

ok? clear? The main issue then is that you have to assure that the user inputs an equal amount of numbers in each textbox, otherwise you have that mismatch between the vectors, and they won't compute: [1 4 5].*[6 7] will error