0

我的代码中有一个子例程,我在其中创建了一个 GUI 供用户选择一种分析类型:

    %% Gives user the choice of replacement method

figure('Units','Normalized','Color','w','Position',[.3 .6 .4 .15],...
    'NumberTitle','off','Name','Analysis choice','MenuBar','none');
uicontrol('Style','text','Units','Normalized',...
    'Position',[.1 .7 .8 .2],'BackgroundColor','w','String','What replacement method do you want to use?');
uicontrol('Style','pushbutton','Units','Normalized',...
    'Position',[.05 .3 .3 .3],'String','Cubic interpolation 24 points',...
    'CallBack','cubic_choice'); % cubic_choice.m rotine must be on current folder
uicontrol('Style','pushbutton','Units','Normalized',...
    'Position',[.4 .3 .3 .3],'String','Last good data value',...
    'CallBack','lgv_choice'); % lgv_choice.m rotine must be on current folder
uicontrol('Style','pushbutton','Units','Normalized',...
    'Position',[.75 .3 .2 .3],'String','Linear interpolation',...
    'CallBack','li_choice'); % li_choice.m rotine must be on current folder
uiwait;
close;

在代码中,我有一个 if 循环来分析用户做出的选择:

if strcmp(inp,'cubic') ...

问题是,当我按下“三次插值 24 点”按钮时,回调函数没有给我inp变量,即它没有出现在工作区上。回调函数是这样的:

%% Callback script for replacement method

% Cubic interpolation with 24 points method

function [inp] = cubic_choice

inp = 'cubic';
uiresume(gcbf); % resumes the button call

我知道我可能必须使用 setappdata 和 getappdata,因为我已经在其他线程中阅读过它,但我无法让它工作。

谁能帮我?

提前致谢。

亲切的问候,佩德罗·桑切斯

4

2 回答 2

1

getappdata恕我直言,您应该检查函数,setappdata和/或,而不是使用全局变量guidata

基本上,从您的回调中,您必须在某个地方设置您的选择,您可以在其余代码中访问它。例如,一种可能性是使用set/getappdata如下:

function cubic_choice()
   figHandle = ancestor(gcbf, 'figure');
   setappdata(figHandle, 'choice', 'cubic');
   uiresume(gcbf);
end

在您调用之后,您可以从示例第一行中的图形调用的返回值中uiwait获取此属性:figHandle

inp = getappdata(figHandle, 'choice');
于 2013-11-05T19:10:39.460 回答
0

这是可变范围和设计的问题。inp在回调函数之外将不可见。您也不是“将其返回到工作区”,因为它是 GUI 将您召回;没有分配发生在工作区。

您可以global inp在工作区回调函数中声明,如

function [inp] = cubic_choice
global inp
inp = 'cubic';

但是,您可能需要考虑直接从回调处理程序中响应选择。这意味着,你在if-statement 中的任何代码都可以直接进入你的回调函数。

或者,如果真的是关于选择,为什么不使用单选按钮呢?然后,您可以保留他h返回的句柄,uicontrol并在以后随时使用get(h, 'value').

于 2013-11-05T18:50:16.787 回答