Problem trying to create a variable accessible by all functions
5 Ansichten (letzte 30 Tage)
Ältere Kommentare anzeigen
I have a GUI that reads excel data and plots the data. The data to plot is chosen by the drop down menus on the x and y axes. I am trying to create a new figure each time the gui plots new data and have the x and y axes labeled with the correct variable. A figure is created but the axes are not labeled. Currently when i get all the variables from the excel sheet in the function setPopupmenuString i am trying to create a global variable z, so when the function updateAxis is called i can access the correct label for the axes. When i try this, it says there is no variable z. Here is my Code.

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', @updateAxes)
set(handles.popupmenuY, 'callback', @updateAxes)
set(handles.radiobuttonRaw,'callback', @updateAxes)
set(handles.radiobuttonNormalized,'callback', @updateAxes)
function setPopupmenuString(hObject,eventdata,handles)
global z
fileName = handles.fileName;
[numbers,colNames] = xlsread(fileName);
z = [numbers,colNames];
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 = guidata(hObject);
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)
figure()
plot(x,y)
xlabel(z(xColNum))
ylabel(z(yColNum))
0 Kommentare
Antworten (1)
Jan
am 22 Aug. 2017
Avoid global variables. Imagine what happens, if you use different GUIs or functions, which use "z" as a global variable: The confusion is perfect.
It would be better to store and share "z" in the handles struct, because it is available in the different functions already.
But of course globals are working. You have to define "z" as global in each function you want to use it. I guess, you get the error inside the updateAxes function. Then add this there:
global z
4 Kommentare
Jan
am 23 Aug. 2017
@Cory:
handles = guidata(hObject); % Get current struct from figure
handles.z = z; % Append z
guidata(hObject, handles); % Store struct in figure again
Walter Roberson
am 23 Aug. 2017
For further emphasis:
MATLAB does not support the possibility of having a variable that can be read from and written to in all routines without the routine having taken special action to allow the variable to be accessed (such as using a "global" statement.)
Siehe auch
Kategorien
Mehr zu Interactive Control and Callbacks finden Sie in Help Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!