how can i solve this 'Inner matrix dimensions must agree.' error?
1 view (last 30 days)
Show older comments
hello,i'm trying to make some gui that shows graph with variables a,b
but i'm matlab newb and have no idea what's wrong with my work
function varargout = untitled(varargin)
gui_Singleton = 1;
gui_State = struct('gui_Name', mfilename, ...
'gui_Singleton', gui_Singleton, ...
'gui_OpeningFcn', @untitled_OpeningFcn, ...
'gui_OutputFcn', @untitled_OutputFcn, ...
'gui_LayoutFcn', [] , ...
'gui_Callback', []);
if nargin && ischar(varargin{1})
gui_State.gui_Callback = str2func(varargin{1});
end
if nargout
[varargout{1:nargout}] = gui_mainfcn(gui_State, varargin{:});
else
gui_mainfcn(gui_State, varargin{:});
end
function untitled_OpeningFcn(hObject, eventdata, handles, varargin)
handles.output = hObject;
guidata(hObject, handles);
global a b
t = 0:0.001:10;
A = a*sin(t*b);
plot(t,A)
function varargout = untitled_OutputFcn(hObject, eventdata, handles)
varargout{1} = handles.output;
function slider1_Callback(hObject, eventdata, handles)
a = get(handles.slider1,'Value');
function slider1_CreateFcn(hObject, eventdata, handles)
if isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
set(hObject,'BackgroundColor',[.9 .9 .9]);
end
function slider2_Callback(hObject, eventdata, handles)
b = get(handles.slider2,'Value');
function slider2_CreateFcn(hObject, eventdata, handles)
if isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
set(hObject,'BackgroundColor',[.9 .9 .9]);
end
0 Comments
Accepted Answer
James Tursa
on 27 Mar 2015
Edited: James Tursa
on 27 Mar 2015
Put global statement in your functions that set a and b so their values get back to your plotting function.
function slider1_Callback(hObject, eventdata, handles)
global a
a = get(handles.slider1,'Value');
function slider2_Callback(hObject, eventdata, handles)
global b
b = get(handles.slider2,'Value');
Or be consistent with your use of the end statement to end function definitions to make sure nested functions share the same variables.
2 Comments
James Tursa
on 27 Mar 2015
Edited: James Tursa
on 27 Mar 2015
First you need to decide how (or if) you are nesting your functions. Then you can address the global issue. In your above code, you have some functions with an end statement and other functions without end statements. This is confusing at best and you should fix this to be consistent with your nesting intent. You also need to make sure your functions that set a and b get called before your function that uses a and b. Or have your plotting function do nothing if either a or b is empty.
I guess I should explicitly point out that the error you are getting is because b is likely empty (size 0 x 0) and you are multiplying it by a non-empty vector t.
More Answers (0)
See Also
Categories
Find more on Annotations in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!