MATLAB: Help in the model!

guimatlab gui

I'm trying to create a GUI with 2 axes but i'm having some problems. Outside the gui, the line runs just fine.
Here is my code and the error:
'''
function pushbutton1_Callback(hObject, eventdata, handles)
p=60; %iterações
aR=str2num(char(get(handles.edit1,'String')));
pR=str2num(char(get(handles.edit2,'String')));
aJ=str2num(char(get(handles.edit3,'String')));
pJ=str2num(char(get(handles.edit4,'String')));
R=zeros(1,p);
R(1) = 0.1;
J=zeros(1,p);
J(1) = 0.1;
for i = 2:p
R(i)= (aR*R(i-1) + pR*J(i-1));
J(i)= (aJ*J(i-1) + pJ*R(i-1));
end
axes(handles.axes1);
plot(R,'Marker','o','MarkerFaceColor','b' );
hold on;
plot(J,'Marker','o','MarkerFaceColor','g' );
axes(handles.axes2);
plot(R,J,'Marker','o','MarkerFaceColor','b' );
'''
And returns the error:
"In an assignment A(I) = B, the number of elements in B and I must be the same.
Error in Analise2Var>pushbutton1_Callback (line 94)
R(i)= (aR*R(i-1) + pR*J(i-1));"

Best Answer

str2num() can return a vector. str2num() is equivalent to eval() of the string surrounded by '[' and ']'. So for example if the string was '3 pi' then the '3' would get parsed as one number and the 'pi' would get parsed as the function pi() and called, returning a number, and you would get back a vector of two values where you might have been only expecting to get back the 3.
str2num() can also return an empty array -- for example if the String property is empty. Empty arrays can cause "number of elements must be the same" problems that are often not at all obvious. For example,
a + 2 * b
will return [] if either a or b are empty. empty plus non-empty always gives an empty result.
I notice you using char() of your edit handles. If your Max property of the edit handles is 1 (the default) and if you have never stored a cell array of string in the String property, then when you get() the String property from the uicontrol('style', 'edit') then the output will be a string and you do not need to char() that (but it would not hurt.) This will be the case whether the user edited the content or not.
If, though, your Max property of the edit handles is more than 1, thereby allowing multiple lines in the edit box, then if you stored a string in the String property and the user has not modified the string, then you will get back a string for the String property. If, though, the user edited the contents when Max > 1 then even if you stored a string, you might get back a cell array of strings. Therefore, if Max > 1, it is safer to either char() the String property to force it to become char, or else cellstr() it to force it to become a cell array of string.
If you are only expecting a single numeric value in the edit box, and do not want to allow the user to enter expressions such as 'pi', then I always recommend switching to str2double(), which is more secure, and also which can handle either strings or cell array of strings (so you would not need to char() or cellstr() before calling str2double()). str2double() will return NaN if the string cannot be interpreted as a single (possibly complex) numeric value. str2double() of the empty string returns nan.