pmmsanches
pmmsanches

Reputation: 3

Use the output of a callback further in the code

I have a subroutine in my code where I create a GUI for the user to chose a type of analysis:

    %% 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;

Further in the code, I have a if loop that analysis the choice that the user made:

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

The problem is, when I press the button "Cubic interpolation 24 points", the callback function doesn't give me the inp variable, i.e., it doesn't appear on the workspace. The callback function is something like this:

%% Callback script for replacement method

% Cubic interpolation with 24 points method

function [inp] = cubic_choice

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

i know that I probably have to use setappdata and getappdata because I already read it in some other thread but I can't get it to work.

Can anyone help me?

Thanks in advance.

Kind regards, Pedro Sanches

Upvotes: 0

Views: 718

Answers (2)

s.bandara
s.bandara

Reputation: 5664

This is an issue of variable scope and design. inp will not be visible outside the callback function. You are also not "returning it to the workspace" because it is the GUI that is calling you back; no assignment takes place in the workspace.

You could declare global inp both in your workspace and in the callback function, as in

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

However, you may want to consider responding to the choice directly from within the callback handler. This means, whatever code you have in your if-statement may just as well go directly into your callback function.

Alternatively, if it is really about choices, why not use radio buttons? Then, you could hold on to he handle h returned by uicontrol and query it's state at any later time with get(h, 'value').

Upvotes: 0

sebastian
sebastian

Reputation: 9696

Rather than using a global variable, you imho should check the functions getappdata, setappdata and / or guidata.

Basically, from your callback, you'll have to set your choice at some place you can access it in the rest of the code. One possibility e.g. is to use set/getappdata as follows:

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

Right after your uiwait call you can then get this property, taking figHandle from the return-value of your figure-call in the first line of your example:

inp = getappdata(figHandle, 'choice');

Upvotes: 1

Related Questions