I made a Matlab GUI through guide, and almost everything is working but I think something is wrong with my callbacks. Any help would be greatly appreciated.

1 vue (au cours des 30 derniers jours)
Its a simple gui that selects an excel file and you are able to plot different variables of data vs each other. I am able to select the excel file and the dropdown boxes become populated with variables, but when i click which variable i want, it doesn't plot it and in the command window this pops up: ans = @(hObject,eventdata)MainGUI('updateAxes',hObject,eventdata,guidata(hObject))
this is my code, i believe its something with the callback.
% --- Executes on button press in pushbuttonLoadXLS.
function pushbuttonLoadXLS_Callback(hObject, eventdata, handles)
% hObject handle to pushbuttonLoadXLS (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
handles.fileName = uigetfile('*.xls');
guidata(hObject,handles)
setPopupmenuString(handles.popupmenuX,eventdata,handles)
setPopupmenuString(handles.popupmenuY,eventdata,handles)
set(handles.popupmenuX,'callback','@(hObject,eventdata)MainGUI(''updateAxes'',hObject,eventdata,guidata(hObject))')
set(handles.popupmenuY,'callback','@(hObject,eventdata)MainGUI(''updateAxes'',hObject,eventdata,guidata(hObject))')
set(handles.radiobuttonRaw,'callback','@(hObject,eventdata)MainGUI(''updateAxes'',hObject,eventdata,guidata(hObject))')
set(handles.radiobuttonNormalized,'callback','@(hObject,eventdata)MainGUI(''updateAxes'',hObject,eventdata,guidata(hObject))')
function setPopupmenuString(hObject,eventdata,handles)
fileName = handles.fileName;
[numbers,colNames] = xlsread(fileName);
set(hObject,'string',colNames);
function [x,y] = readExcelColumns(fileName,xColNum,yColNum)
a = xlsread(fileName);
x = a(:,xColNum); % make time be x axis for data
y = a(:,yColNum); % put data in y axis
function updateAxes(hObject,eventdata,handles)
xColNum = get(handles.popupmenuX,'value');
yColNum = get(handles.popupmenuY,'value');
fileName = handles.fileName;
[x,y] = readExcelColumns(fileName,xColNum,yColNum);
flagWantsNormalized = ...
get(handles.radiobuttonNormalized,'value');
if flagWantsNormalized
y = (y - min(y)) / range(y);
end
plot(handles.axes1,x,y)
  2 commentaires
Jan
Jan le 21 Août 2017
The code is not readable, if you do not format it using the "{} Code" button. The description "but when i click which variable i want, it doesn't plot it" is not clear enough. Please post more details.
Cory Karnick
Cory Karnick le 21 Août 2017
I'm sorry, first time posting. I fixed the formatting i believe. When i run the gui, the window pops up and i click the push button Load.xls, and select my file, the code loads the file and all the variables that were in that excel file get sent to the drop down menu, but the variables in the drop down menu never get plotted.

Connectez-vous pour commenter.

Réponse acceptée

Jan
Jan le 21 Août 2017
Modifié(e) : Jan le 21 Août 2017
It is a very ugly idea, to define a callback like this:
'@(hObject,eventdata)MainGUI(''updateAxes'',hObject,eventdata,guidata(hObject))'
This is a string, which is interpreted in the base workspace and defines an anonymous function there. Why do you use such an obfuscated and indirect method to call a callback? Prefer:
set(handles.popupmenuX, 'callback', @updateAxes)
and get the current value of the handles struct there:
function updateAxes(hObject, EventData)
handles = guidata(hObject);
...
It looks strange, that the Excel file is read multiple times. Reading in once and storing the data in the handles struct will be more convenient. But I'm not sure, what happens in the code exactly, because it is hard to read without a proper formatting.

Plus de réponses (0)

Catégories

En savoir plus sur Loops and Conditional Statements dans Help Center et File Exchange

Produits

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by