将参数调用到其他函数 Octave GNU?
Call Argument into other function Octave GNU?
我将 Octave GNU 用于 GUI Excel 数据。
我想问你们如何在另一个函数中调用定义的参数。
这是我的代码。
%%First Function = pushbutton_Callback
function pushbutton_Callback(hObject, eventdata, handles)
fileName = uigetfile('*.xlsx')%%excel data import
handles.fileName=fileName;
guidata(hObject, handles)
endfunction
%%Second Function = popupmenuX_Callback
function popupmenuX_Callback(hObject, eventdata, handles)
fileName = fileName @pushbutton_Callback;
printf ("in popupmenuX_Callback, calling pushbutton_Callback\n");
%%To nested function to prevent (because in Octave nested function is not accepted), I used subfunction as alternative%%
handles.fileName=fileName; %% This argument fileName I want to bring from first function
[numbers, colNames]=xlsread(fileName); %%read columns of excel data in first function
set(hObject,'string',colNames);
endfunction
如果我这样放置我的函数,总是会出现这些错误。
>> fileName = V8.xlsx
error: superclass calls can only occur in methods or constructors
error: called from
popupmenuX_Callback at line 61 column 10
所以我想做的是,我想将第一个函数 (pushbutton_Callback) 中定义的参数“文件名”带到第二个函数 (popupX_callback) 中。但它不能在第二个函数中定义。
我听说八度中的 nested function 可以用“foo”、“foobar”或“ex_top”、“ex_a”函数来解析。但我不能不解决“ex_”功能的问题。那么我是否必须使用“foo”,“foobar”函数将参数调用到其他函数中?
此致!
=========================================== ==============================
我用我的完整代码编辑了我的问题。
(下面的完整代码)
所以我想做的就是,像这样video。但在 Matlab 中,就像在视频中一样,它可以使用 GUIDE 或应用程序设计器制作,但在 Octave 中没有这样的功能。所以作为一个八度初学者,我很难解决这个问题。
%%Versuch
%% Diagramm zeichen
%%============================================================================
close all
clear h
graphics_toolkit qt
pkg load io
%%Uicontrols
%%Graph
h.ax = axes ("position", [0.3 0.25 0.6 0.5]);
%%Title
h.plot_title_label = uicontrol ("style", "text",
"units", "normalized",
"string", "Versuchsergebnis",
"horizontalalignment", "left",
"position", [0.03 0.9 0.25 0.08]);
%% Design for excel data import
h.print_pushbutton = uicontrol ("style", "pushbutton",
"units", "normalized",
"string", "Excel Datei mitbringen",
"callback", @pushbutton_Callback,
"position", [0.03 0.8 0.3 0.09]);
%% Drawing axis
h.popupmenuX = uicontrol("Style","popupmenu",
"units", "normalized",
"string","X Axis",...
"callback", @popupmenuX_Callback,
"Position",[0.7 0.04 0.2 0.05]);
h.popupmenuY = uicontrol("Style","popupmenu",
"units", "normalized",
"string","Y Axis",
"callback",@popupmenuY_Callback,
"Position",[0.03 0.5 0.2 0.05]);
%%=============================================================================
%% Functions
%%=============================================================================
%% 1. Excel Data import
function pushbutton_Callback(hObject, eventdata, handles)
fileName = uigetfile('*.xlsx')%%excel data import
handles.fileName = fileName;
guidata(hObject, handles)
endfunction
%% 2. X Axis Information from excel data import
function popupmenuX_Callback(hObject, eventdata, handles)
fileName = pushbutton_Callback(hObject, eventdata, handles)
%%This code fileName causes error, that 'handles' is not defined.%%
handles.fileName = fileName; %% This argument fileName I want to bring from first function
[numbers, colNames] = xlsread(fileName); %%read columns of excel data in first function
set(hObject,'string',colNames);
endfunction
%% 3. Y Axis Information from excel data import
function popupmenuY_Callback(hObject, eventdata, handles)
filename = pushbutton_Callback(hObject, eventdata, handles)
handles.fileName = fileName;
[numbers, colNames] = xlsread(fileName);
set(hObject,'string',colNames);
endfunction
%%%% Plot the graph
a = xlsread (fileName);
xColNum = get(popupmenuX_Callback,'value');
yColNum = get(popupmenuY_Callback,'value');
fileName = handles.fileName;
x = a(:,xColNum);
y = a(:,yColNum);
h.ax = plot(x,y);
行
fileName = fileName @pushbutton_Callback;
并不像你想象的那样。
说明背景有点复杂,不过基本上在matlab中,new-style classes create with the classdef keyword, which also allow inheritance, allow you call a method 'parent' class(或“superclass”)通过 methodname@parentclassname(args)
语法。为了与 matlab 兼容,Octave 也将这个系统移植到了 Octave。你得到的错误实际上是说你试图在一个没有意义的上下文中调用一个 superclass 方法(这是真的,因为你不在 classdef 块中).
这解释了为什么会出现该错误。
现在,就您尝试做的事情而言,我认为您基本上只想从 popupmenuX_Callback
中调用 pushbutton_Callback
函数】 一个吧?所以只要这样做,用适当的参数,例如:
filename = pushbutton_Callback(hObject, eventdata, handles)
[numbers, colNames] = xlsread(fileName);
PS。我建议您总是在函数内缩进代码。它看起来更清晰,有助于发现错误!
允许加载 .csv 文件并根据 csv 填充两个弹出菜单的 GUI 示例 headers。
示例 data.csv 文件:
Col1, Col2, Col3
1, 2, 3
2, 4, 8
3, 8, 9
示例myscript.m
pkg load io
% Create the Gui Window which will hold all controls and relevant data.
GuiWindow = figure()
% An 'axes' object for displaying plots in the Gui Window
axes ("position", [0.3 0.25 0.6 0.5], 'tag', 'plotarea' );
% Static text element used as a title
uicontrol ("style", "text", "units", "normalized", "string", "Fur Zoo Hair Geb knees.", "horizontalalignment", "left", "position", [0.03 0.9 0.3 0.08] );
% A button for importing (excel) data
uicontrol ("style", "pushbutton", "units", "normalized", "string", "CSV Dat Eye Meat Bringen", "callback", { @pushbutton_Callback, GuiWindow }, "position", [0.03 0.8 0.35 0.09], 'tag', 'button' );
% Popupmenus for selecting appropriate X and Y axis to display in plots
uicontrol("Style","popupmenu", "units", "normalized", "string","X Axis", "callback", { @popupmenuX_Callback, GuiWindow }, "Position",[0.7 0.04 0.2 0.05], 'tag', 'XAxisMenu' );
uicontrol("Style","popupmenu", "units", "normalized", "string","Y Axis", "callback", { @popupmenuY_Callback, GuiWindow }, "Position",[0.03 0.5 0.2 0.05], 'tag', 'YAxisMenu' );
%%=============================================================================
%% Functions (preferably placed in their own files!)
%%=============================================================================
function pushbutton_Callback(hObject, eventdata, GuiWindow)
% Read in data from file, graphically selected by user
fileName = uigetfile('*.csv');
CellCsv = csv2cell( fileName );
Header = CellCsv(1, :);
Data = CellCsv(2:end, :);
% Populate the menu items for the X and Y Axis from the csv header
XAxisMenu = findobj( 'tag', 'XAxisMenu' );
set( XAxisMenu, 'string', Header );
YAxisMenu = findobj( 'tag', 'YAxisMenu' );
set( YAxisMenu, 'string', Header );
% Also store headers and data as GuiWindow app data, in case we need them again later.
setappdata( GuiWindow, 'Header', Header );
setappdata( GuiWindow, 'Data' , Data );
% Plot a preliminary plot in the plot area
XData = [Data{:, 1}];
YData = [Data{:, 1}];
plot( XData, YData, 'bo-', 'tag', 'plotobject' );
endfunction
%% 2. X Axis Information from excel data import
function popupmenuX_Callback( hObject, eventdata, GuiWindow )
Axes = findobj( 'tag', 'plotarea' );
Selection = get( hObject, 'value' );
XData = [ getappdata( GuiWindow, 'Data' ){ :, Selection } ];
PlotObj = findobj( 'tag', 'plotobject' );
set( PlotObj, 'xdata', XData )
endfunction
%% 3. Y Axis Information from excel data import
function popupmenuY_Callback( hObject, eventdata, GuiWindow )
Axes = findobj( 'tag', 'plotarea' );
Selection = get( hObject, 'value' );
YData = [ getappdata( GuiWindow, 'Data' ){ :, Selection } ];
PlotObj = findobj( 'tag', 'plotobject' );
set( PlotObj, 'ydata', YData )
endfunction
这演示了在回调中访问其他图形 object 的两种方法。一种是提供 object 作为参数(例如,GuiWindow),另一种是提供 'tags',可用于通过 findobj
函数识别 object。
请注意,我仅将数据存储在 GuiWindow object 中,而不是每个单独的图形 object(如按钮等)。另外,我更喜欢 setappdata,因为它允许在单独的名称下存储多个数据(而 guidata 只存储一个 object,尽管这可以是一个结构)。
上面是 Tasos 的答案(带有 csv 文件),我将他的答案应用于我的代码,该代码与 excel 文件一起使用。因为代码 xlsread
忽略了列,所以用户应该注意这一点。
再次感谢Tasos!
pkg load io
% Create the Gui Window which will hold all controls and relevant data.
GuiWindow = figure()
% An 'axes' object for displaying plots in the Gui Window
axes ("position", [0.3 0.25 0.6 0.5], 'tag', 'plotarea' );
% Static text element used as a title
uicontrol ("style", "text", "units", "normalized", "string", "Versuchsergebnis",'ForegroundColor','w','BackgroundColor',[0 0.4470 0.7410],'Fontweight','bold', "horizontalalignment", "center", "position", [0.03 0.9 0.35 0.08] );
% A button for importing (excel) data
uicontrol ("style", "pushbutton", "units", "normalized", "string", "Datei(xlsx) mitbringen", "callback", { @pushbutton_Callback, GuiWindow }, "position", [0.03 0.8 0.35 0.09], 'tag', 'button' );
% Popupmenus for selecting appropriate X and Y axis to display in plots
uicontrol("Style","popupmenu", "units", "normalized", "string","X Axis", "callback", { @popupmenuX_Callback, GuiWindow }, "Position",[0.7 0.04 0.2 0.05], 'tag', 'XAxisMenu' );
uicontrol("Style","popupmenu", "units", "normalized", "string","Y Axis", "callback", { @popupmenuY_Callback, GuiWindow }, "Position",[0.03 0.5 0.2 0.05], 'tag', 'YAxisMenu' );
%%=============================================================================
%% Functions (preferably placed in their own files!)
%%=============================================================================
function pushbutton_Callback(hObject, eventdata, GuiWindow)
% Read in data from file, graphically selected by user
fileName = uigetfile('*.xlsx');
[num,txt,raw] = xlsread(fileName);
header = raw(1,:);
Data = xlsread(fileName);
% Show fileName
button = findobj('tag', 'button');
set( button, 'string', fileName)
% Populate the menu items for the X and Y Axis from the csv header
XAxisMenu = findobj( 'tag', 'XAxisMenu' );
set( XAxisMenu, 'string', header );
YAxisMenu = findobj( 'tag', 'YAxisMenu' );
set( YAxisMenu, 'string', header );
% Also store headers and data as GuiWindow app data, in case we need them again later.
setappdata( GuiWindow, 'header', header );
setappdata( GuiWindow, 'Data' , Data );
% Plot a preliminary plot in the plot area
XData = Data(:, 1);
YData = Data(:, 1);
plot( XData, YData, 'tag', 'plotobject' );
endfunction
%% 2. X Axis Information from excel data import
function popupmenuX_Callback( hObject, eventdata, GuiWindow )
Axes = findobj( 'tag', 'plotarea' );
Selection = get( hObject, 'value' );
XData = [ getappdata( GuiWindow, 'Data' )( :, Selection ) ];
PlotObj = findobj( 'tag', 'plotobject' );
set( PlotObj, 'xdata', XData )
endfunction
%% 3. Y Axis Information from excel data import
function popupmenuY_Callback( hObject, eventdata, GuiWindow )
Axes = findobj( 'tag', 'plotarea' );
Selection = get( hObject, 'value' );
YData = [ getappdata( GuiWindow, 'Data' )( :, Selection ) ];
PlotObj = findobj( 'tag', 'plotobject' );
set( PlotObj, 'ydata', YData )
endfunction
我将 Octave GNU 用于 GUI Excel 数据。 我想问你们如何在另一个函数中调用定义的参数。 这是我的代码。
%%First Function = pushbutton_Callback
function pushbutton_Callback(hObject, eventdata, handles)
fileName = uigetfile('*.xlsx')%%excel data import
handles.fileName=fileName;
guidata(hObject, handles)
endfunction
%%Second Function = popupmenuX_Callback
function popupmenuX_Callback(hObject, eventdata, handles)
fileName = fileName @pushbutton_Callback;
printf ("in popupmenuX_Callback, calling pushbutton_Callback\n");
%%To nested function to prevent (because in Octave nested function is not accepted), I used subfunction as alternative%%
handles.fileName=fileName; %% This argument fileName I want to bring from first function
[numbers, colNames]=xlsread(fileName); %%read columns of excel data in first function
set(hObject,'string',colNames);
endfunction
如果我这样放置我的函数,总是会出现这些错误。
>> fileName = V8.xlsx
error: superclass calls can only occur in methods or constructors
error: called from
popupmenuX_Callback at line 61 column 10
所以我想做的是,我想将第一个函数 (pushbutton_Callback) 中定义的参数“文件名”带到第二个函数 (popupX_callback) 中。但它不能在第二个函数中定义。 我听说八度中的 nested function 可以用“foo”、“foobar”或“ex_top”、“ex_a”函数来解析。但我不能不解决“ex_”功能的问题。那么我是否必须使用“foo”,“foobar”函数将参数调用到其他函数中?
此致!
=========================================== ==============================
我用我的完整代码编辑了我的问题。 (下面的完整代码) 所以我想做的就是,像这样video。但在 Matlab 中,就像在视频中一样,它可以使用 GUIDE 或应用程序设计器制作,但在 Octave 中没有这样的功能。所以作为一个八度初学者,我很难解决这个问题。
%%Versuch
%% Diagramm zeichen
%%============================================================================
close all
clear h
graphics_toolkit qt
pkg load io
%%Uicontrols
%%Graph
h.ax = axes ("position", [0.3 0.25 0.6 0.5]);
%%Title
h.plot_title_label = uicontrol ("style", "text",
"units", "normalized",
"string", "Versuchsergebnis",
"horizontalalignment", "left",
"position", [0.03 0.9 0.25 0.08]);
%% Design for excel data import
h.print_pushbutton = uicontrol ("style", "pushbutton",
"units", "normalized",
"string", "Excel Datei mitbringen",
"callback", @pushbutton_Callback,
"position", [0.03 0.8 0.3 0.09]);
%% Drawing axis
h.popupmenuX = uicontrol("Style","popupmenu",
"units", "normalized",
"string","X Axis",...
"callback", @popupmenuX_Callback,
"Position",[0.7 0.04 0.2 0.05]);
h.popupmenuY = uicontrol("Style","popupmenu",
"units", "normalized",
"string","Y Axis",
"callback",@popupmenuY_Callback,
"Position",[0.03 0.5 0.2 0.05]);
%%=============================================================================
%% Functions
%%=============================================================================
%% 1. Excel Data import
function pushbutton_Callback(hObject, eventdata, handles)
fileName = uigetfile('*.xlsx')%%excel data import
handles.fileName = fileName;
guidata(hObject, handles)
endfunction
%% 2. X Axis Information from excel data import
function popupmenuX_Callback(hObject, eventdata, handles)
fileName = pushbutton_Callback(hObject, eventdata, handles)
%%This code fileName causes error, that 'handles' is not defined.%%
handles.fileName = fileName; %% This argument fileName I want to bring from first function
[numbers, colNames] = xlsread(fileName); %%read columns of excel data in first function
set(hObject,'string',colNames);
endfunction
%% 3. Y Axis Information from excel data import
function popupmenuY_Callback(hObject, eventdata, handles)
filename = pushbutton_Callback(hObject, eventdata, handles)
handles.fileName = fileName;
[numbers, colNames] = xlsread(fileName);
set(hObject,'string',colNames);
endfunction
%%%% Plot the graph
a = xlsread (fileName);
xColNum = get(popupmenuX_Callback,'value');
yColNum = get(popupmenuY_Callback,'value');
fileName = handles.fileName;
x = a(:,xColNum);
y = a(:,yColNum);
h.ax = plot(x,y);
行
fileName = fileName @pushbutton_Callback;
并不像你想象的那样。
说明背景有点复杂,不过基本上在matlab中,new-style classes create with the classdef keyword, which also allow inheritance, allow you call a method 'parent' class(或“superclass”)通过 methodname@parentclassname(args)
语法。为了与 matlab 兼容,Octave 也将这个系统移植到了 Octave。你得到的错误实际上是说你试图在一个没有意义的上下文中调用一个 superclass 方法(这是真的,因为你不在 classdef 块中).
这解释了为什么会出现该错误。
现在,就您尝试做的事情而言,我认为您基本上只想从 popupmenuX_Callback
中调用 pushbutton_Callback
函数】 一个吧?所以只要这样做,用适当的参数,例如:
filename = pushbutton_Callback(hObject, eventdata, handles)
[numbers, colNames] = xlsread(fileName);
PS。我建议您总是在函数内缩进代码。它看起来更清晰,有助于发现错误!
允许加载 .csv 文件并根据 csv 填充两个弹出菜单的 GUI 示例 headers。
示例 data.csv 文件:
Col1, Col2, Col3
1, 2, 3
2, 4, 8
3, 8, 9
示例myscript.m
pkg load io
% Create the Gui Window which will hold all controls and relevant data.
GuiWindow = figure()
% An 'axes' object for displaying plots in the Gui Window
axes ("position", [0.3 0.25 0.6 0.5], 'tag', 'plotarea' );
% Static text element used as a title
uicontrol ("style", "text", "units", "normalized", "string", "Fur Zoo Hair Geb knees.", "horizontalalignment", "left", "position", [0.03 0.9 0.3 0.08] );
% A button for importing (excel) data
uicontrol ("style", "pushbutton", "units", "normalized", "string", "CSV Dat Eye Meat Bringen", "callback", { @pushbutton_Callback, GuiWindow }, "position", [0.03 0.8 0.35 0.09], 'tag', 'button' );
% Popupmenus for selecting appropriate X and Y axis to display in plots
uicontrol("Style","popupmenu", "units", "normalized", "string","X Axis", "callback", { @popupmenuX_Callback, GuiWindow }, "Position",[0.7 0.04 0.2 0.05], 'tag', 'XAxisMenu' );
uicontrol("Style","popupmenu", "units", "normalized", "string","Y Axis", "callback", { @popupmenuY_Callback, GuiWindow }, "Position",[0.03 0.5 0.2 0.05], 'tag', 'YAxisMenu' );
%%=============================================================================
%% Functions (preferably placed in their own files!)
%%=============================================================================
function pushbutton_Callback(hObject, eventdata, GuiWindow)
% Read in data from file, graphically selected by user
fileName = uigetfile('*.csv');
CellCsv = csv2cell( fileName );
Header = CellCsv(1, :);
Data = CellCsv(2:end, :);
% Populate the menu items for the X and Y Axis from the csv header
XAxisMenu = findobj( 'tag', 'XAxisMenu' );
set( XAxisMenu, 'string', Header );
YAxisMenu = findobj( 'tag', 'YAxisMenu' );
set( YAxisMenu, 'string', Header );
% Also store headers and data as GuiWindow app data, in case we need them again later.
setappdata( GuiWindow, 'Header', Header );
setappdata( GuiWindow, 'Data' , Data );
% Plot a preliminary plot in the plot area
XData = [Data{:, 1}];
YData = [Data{:, 1}];
plot( XData, YData, 'bo-', 'tag', 'plotobject' );
endfunction
%% 2. X Axis Information from excel data import
function popupmenuX_Callback( hObject, eventdata, GuiWindow )
Axes = findobj( 'tag', 'plotarea' );
Selection = get( hObject, 'value' );
XData = [ getappdata( GuiWindow, 'Data' ){ :, Selection } ];
PlotObj = findobj( 'tag', 'plotobject' );
set( PlotObj, 'xdata', XData )
endfunction
%% 3. Y Axis Information from excel data import
function popupmenuY_Callback( hObject, eventdata, GuiWindow )
Axes = findobj( 'tag', 'plotarea' );
Selection = get( hObject, 'value' );
YData = [ getappdata( GuiWindow, 'Data' ){ :, Selection } ];
PlotObj = findobj( 'tag', 'plotobject' );
set( PlotObj, 'ydata', YData )
endfunction
这演示了在回调中访问其他图形 object 的两种方法。一种是提供 object 作为参数(例如,GuiWindow),另一种是提供 'tags',可用于通过 findobj
函数识别 object。
请注意,我仅将数据存储在 GuiWindow object 中,而不是每个单独的图形 object(如按钮等)。另外,我更喜欢 setappdata,因为它允许在单独的名称下存储多个数据(而 guidata 只存储一个 object,尽管这可以是一个结构)。
上面是 Tasos 的答案(带有 csv 文件),我将他的答案应用于我的代码,该代码与 excel 文件一起使用。因为代码 xlsread
忽略了列,所以用户应该注意这一点。
再次感谢Tasos!
pkg load io
% Create the Gui Window which will hold all controls and relevant data.
GuiWindow = figure()
% An 'axes' object for displaying plots in the Gui Window
axes ("position", [0.3 0.25 0.6 0.5], 'tag', 'plotarea' );
% Static text element used as a title
uicontrol ("style", "text", "units", "normalized", "string", "Versuchsergebnis",'ForegroundColor','w','BackgroundColor',[0 0.4470 0.7410],'Fontweight','bold', "horizontalalignment", "center", "position", [0.03 0.9 0.35 0.08] );
% A button for importing (excel) data
uicontrol ("style", "pushbutton", "units", "normalized", "string", "Datei(xlsx) mitbringen", "callback", { @pushbutton_Callback, GuiWindow }, "position", [0.03 0.8 0.35 0.09], 'tag', 'button' );
% Popupmenus for selecting appropriate X and Y axis to display in plots
uicontrol("Style","popupmenu", "units", "normalized", "string","X Axis", "callback", { @popupmenuX_Callback, GuiWindow }, "Position",[0.7 0.04 0.2 0.05], 'tag', 'XAxisMenu' );
uicontrol("Style","popupmenu", "units", "normalized", "string","Y Axis", "callback", { @popupmenuY_Callback, GuiWindow }, "Position",[0.03 0.5 0.2 0.05], 'tag', 'YAxisMenu' );
%%=============================================================================
%% Functions (preferably placed in their own files!)
%%=============================================================================
function pushbutton_Callback(hObject, eventdata, GuiWindow)
% Read in data from file, graphically selected by user
fileName = uigetfile('*.xlsx');
[num,txt,raw] = xlsread(fileName);
header = raw(1,:);
Data = xlsread(fileName);
% Show fileName
button = findobj('tag', 'button');
set( button, 'string', fileName)
% Populate the menu items for the X and Y Axis from the csv header
XAxisMenu = findobj( 'tag', 'XAxisMenu' );
set( XAxisMenu, 'string', header );
YAxisMenu = findobj( 'tag', 'YAxisMenu' );
set( YAxisMenu, 'string', header );
% Also store headers and data as GuiWindow app data, in case we need them again later.
setappdata( GuiWindow, 'header', header );
setappdata( GuiWindow, 'Data' , Data );
% Plot a preliminary plot in the plot area
XData = Data(:, 1);
YData = Data(:, 1);
plot( XData, YData, 'tag', 'plotobject' );
endfunction
%% 2. X Axis Information from excel data import
function popupmenuX_Callback( hObject, eventdata, GuiWindow )
Axes = findobj( 'tag', 'plotarea' );
Selection = get( hObject, 'value' );
XData = [ getappdata( GuiWindow, 'Data' )( :, Selection ) ];
PlotObj = findobj( 'tag', 'plotobject' );
set( PlotObj, 'xdata', XData )
endfunction
%% 3. Y Axis Information from excel data import
function popupmenuY_Callback( hObject, eventdata, GuiWindow )
Axes = findobj( 'tag', 'plotarea' );
Selection = get( hObject, 'value' );
YData = [ getappdata( GuiWindow, 'Data' )( :, Selection ) ];
PlotObj = findobj( 'tag', 'plotobject' );
set( PlotObj, 'ydata', YData )
endfunction