保存函数的输出
Save the output of a function
每次按下 space 条时,我已经能够将以下代码放在一起以查找时间戳。但是,我想不出将 S.tm 保存到工作 space 的方法。这是代码:
function main
S.tm = [];
S.cnt = 0;
S.fh = figure('KeyPressFcn',@youPressedSomething,...
'menu','none',...
'pos',[400 400 320 50]);
S.th = uicontrol('Style','text','Position',[10 10 300 30],...
'String','You have not hit space yet');
function youPressedSomething(varargin)
if strcmp(varargin{2}.Character,' ')
S.tm = [S.tm now]
S.cnt = S.cnt + 1;
set(S.th,'str',sprintf('You hit space %i!',S.cnt));
end
end
end
您可以尝试这样编辑您的代码:
function[S.fh]=main()
%% Your Code
function yPS(varargin)
if...
%% Your Code
end
set(S.fh,'UserData',S.tm);
end %yPS
end %main
这应该 return 图处理工作区并将其保存在内存中。然后,每当调用 yPS
函数时,它都会使用图形的共享句柄 S.fh
并(重新)设置 UsedData
属性,它专门用于包含用户数据。
要使用函数调用它 Foo=main
。
get(Foo.fh,'UserData')
ans =
[]
按几次空格键
>> get(AA.fh,'userdata')
ans =
1.0e+005 *
7.3645
7.3645
7.3645
这里有两个主要选项。您可以在 函数中使用 assignin
将数据保存到主工作区。
function main
S.tm = [];
S.cnt = 0;
S.fh = figure('KeyPressFcn',@youPressedSomething,...
'menu','none',...
'pos',[400 400 320 50]);
S.th = uicontrol('Style','text','Position',[10 10 300 30],...
'String','You have not hit space yet');
function youPressedSomething(varargin)
if strcmp(varargin{2}.Character,' ')
S.tm = [S.tm now]
S.cnt = S.cnt + 1;
set(S.th,'str',sprintf('You hit space %i!',S.cnt));
%// Save as "timestamps" in the base workspace
assignin('base', 'timestamps', S.tm);
end
end
end
或者更好的方法是使用 waitfor
来阻止函数的执行,直到图窗关闭。然后你可以 return S.tm
就像一个正常的输出参数
function timestamps = main()
S.tm = [];
S.cnt = 0;
S.fh = figure('KeyPressFcn',@youPressedSomething,...
'menu','none',...
'pos',[400 400 320 50]);
S.th = uicontrol('Style','text','Position',[10 10 300 30],...
'String','You have not hit space yet');
%// Wait until the figure is closed
waitfor(S.fh);
%// Save S.tm as timestamps and return
timestamps = S.tm;
function youPressedSomething(varargin)
if strcmp(varargin{2}.Character,' ')
S.tm = [S.tm now]
S.cnt = S.cnt + 1;
set(S.th,'str',sprintf('You hit space %i!',S.cnt));
%// Save as "timestamps" in the base workspace
assignin('base', 'timestamps', S.tm);
end
end
end
每次按下 space 条时,我已经能够将以下代码放在一起以查找时间戳。但是,我想不出将 S.tm 保存到工作 space 的方法。这是代码:
function main
S.tm = [];
S.cnt = 0;
S.fh = figure('KeyPressFcn',@youPressedSomething,...
'menu','none',...
'pos',[400 400 320 50]);
S.th = uicontrol('Style','text','Position',[10 10 300 30],...
'String','You have not hit space yet');
function youPressedSomething(varargin)
if strcmp(varargin{2}.Character,' ')
S.tm = [S.tm now]
S.cnt = S.cnt + 1;
set(S.th,'str',sprintf('You hit space %i!',S.cnt));
end
end
end
您可以尝试这样编辑您的代码:
function[S.fh]=main()
%% Your Code
function yPS(varargin)
if...
%% Your Code
end
set(S.fh,'UserData',S.tm);
end %yPS
end %main
这应该 return 图处理工作区并将其保存在内存中。然后,每当调用 yPS
函数时,它都会使用图形的共享句柄 S.fh
并(重新)设置 UsedData
属性,它专门用于包含用户数据。
要使用函数调用它 Foo=main
。
get(Foo.fh,'UserData')
ans =
[]
按几次空格键
>> get(AA.fh,'userdata')
ans =
1.0e+005 *
7.3645
7.3645
7.3645
这里有两个主要选项。您可以在 函数中使用 assignin
将数据保存到主工作区。
function main
S.tm = [];
S.cnt = 0;
S.fh = figure('KeyPressFcn',@youPressedSomething,...
'menu','none',...
'pos',[400 400 320 50]);
S.th = uicontrol('Style','text','Position',[10 10 300 30],...
'String','You have not hit space yet');
function youPressedSomething(varargin)
if strcmp(varargin{2}.Character,' ')
S.tm = [S.tm now]
S.cnt = S.cnt + 1;
set(S.th,'str',sprintf('You hit space %i!',S.cnt));
%// Save as "timestamps" in the base workspace
assignin('base', 'timestamps', S.tm);
end
end
end
或者更好的方法是使用 waitfor
来阻止函数的执行,直到图窗关闭。然后你可以 return S.tm
就像一个正常的输出参数
function timestamps = main()
S.tm = [];
S.cnt = 0;
S.fh = figure('KeyPressFcn',@youPressedSomething,...
'menu','none',...
'pos',[400 400 320 50]);
S.th = uicontrol('Style','text','Position',[10 10 300 30],...
'String','You have not hit space yet');
%// Wait until the figure is closed
waitfor(S.fh);
%// Save S.tm as timestamps and return
timestamps = S.tm;
function youPressedSomething(varargin)
if strcmp(varargin{2}.Character,' ')
S.tm = [S.tm now]
S.cnt = S.cnt + 1;
set(S.th,'str',sprintf('You hit space %i!',S.cnt));
%// Save as "timestamps" in the base workspace
assignin('base', 'timestamps', S.tm);
end
end
end