MATLAB: Cant plot function while using buttons

callbackerrorhandlematrixplotworkspace

So i need to do a window with buttons, for example there are 2 buttons, one of them plots one data from 81×1 matrix and another should plot other data from same range but different matrix (81×1) . i used >>guide > Blank GUI (default) for my windows and plot setup.
When i use separate script for plot :
plot(t(:,1),speed(:,1));
xlabel('time');
ylabel('speed');
works perfectly, but when i try to do it in callback for a button
% --- Executes on button press in Greitis.
function Greitis_Callback(hObject, eventdata, handles)
% hObject handle to Greitis (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
axes(handles.axes1);
plot(t(:,1),speed(:,1));
xlabel('time');
ylabel('speed');
window opens. When i press button to plot my data i get this :
>> untitled1
Undefined function or variable 't'.
Error in untitled1>Greitis_Callback (line 107)
plot(t(:,1),speed(:,1));
Error in gui_mainfcn (line 95)
feval(varargin{:});
Error in untitled1 (line 42)
gui_mainfcn(gui_State, varargin{:});
Error in matlab.graphics.internal.figfile.FigFile/read>@(hObject,eventdata)untitled1('Greitis_Callback',hObject,eventdata,guidata(hObject))
Error while evaluating UIControl Callback
im kinda newbie, so need your help there, have a nice day 🙂

Best Answer

at first you need to load, asign and save handles. in my situation it looked like this:
function bandmygtukussugrafiku3_OpeningFcn(hObject, eventdata, handles, varargin)
% This function has no output args, see OutputFcn.
% hObject handle to figure
% eventdata reserved - to be defined in a future version of MATLAB

% handles structure with handles and user data (see GUIDATA)

% varargin command line arguments to bandmygtukussugrafiku3 (see VARARGIN)
% Choose default command line output for bandmygtukussugrafiku3
handles.output = hObject;
%load variables
load('C:\Users\(your data directory here)\time.mat','t')
load('C:\Users\(your data directory here)\Altitude.mat','alt')
%asign to handles
handles.t = t;
handles.alt = alt;
% Update handles structure
guidata(hObject, handles);
now as i had my variables saved as handles i could proceed to button function
function aukstis_Callback(hObject, eventdata, handles)
% hObject handle to aukstis (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
axes(handles.axes1);
t = handles.t(:,1);
alt = handles.alt(:,1);
plot(t,alt);
there i defined variables using handles and declared that it will be data from matrix first column and just simply ploted it.
someone had to answer, so i did :D