0

I have matlab GUI like this, Which has a start pushbutton, update pushbutton and an edit text box.

function varargout = Main_function(varargin)

% Begin initialization code - DO NOT EDIT
gui_Singleton = 1;
gui_State = struct('gui_Name',       mfilename, ...
                   'gui_Singleton',  gui_Singleton, ...
                   'gui_OpeningFcn', @Main_function_OpeningFcn, ...
                   'gui_OutputFcn',  @Main_function_OutputFcn, ...
                   'gui_LayoutFcn',  [] , ...
                   'gui_Callback',   []);
if nargin && ischar(varargin{1})
    gui_State.gui_Callback = str2func(varargin{1});
end

if nargout
    [varargout{1:nargout}] = gui_mainfcn(gui_State, varargin{:});
else
    gui_mainfcn(gui_State, varargin{:});
end
% End initialization code - DO NOT EDIT


% --- Executes just before encdecgui is made visible.
function encdecgui_OpeningFcn(hObject, eventdata, handles, varargin)
% This function has no output args, see OutputFcn.
% hObject    handle to figure
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
% varargin   command line arguments to encdecgui (see VARARGIN)

% Choose default command line output for encdecgui
handles.output = hObject;

% Update handles structure
guidata(hObject, handles);

% UIWAIT makes encdecgui wait for user response (see UIRESUME)
% uiwait(handles.figure1);


% --- Outputs from this function are returned to the command line.
function varargout = encdecgui_OutputFcn(hObject, eventdata, handles) 
% varargout  cell array for returning output args (see VARARGOUT);
% hObject    handle to figure
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Get default command line output from handles structure
varargout{1} = handles.output;


% --- Executes on button press in pb1.
function pb1_Callback(hObject, eventdata, handles)
% hObject    handle to pb1 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
% if (mfilename(external_prog) == 0)
    external_script
% else
%     set(handles.pb1,'enable','off');
% end


% --- Executes on button press in pb2.
function pb2_Callback(hObject, eventdata, handles)
% hObject    handle to pb2 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
mydata = str2double(get(handles.edit1,'string'));
%Update mydata to external_prog's while loop


function edit1_Callback(hObject, eventdata, handles)
% hObject    handle to edit1 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Hints: get(hObject,'String') returns contents of edit1 as text
%        str2double(get(hObject,'String')) returns contents of edit1 as a double


% --- Executes during object creation, after setting all properties.
function edit1_CreateFcn(hObject, eventdata, handles)
% hObject    handle to edit1 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    empty - handles not created until after all CreateFcns called

% Hint: edit controls usually have a white background on Windows.
%       See ISPC and COMPUTER.
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
    set(hObject,'BackgroundColor','white');
end


% --- Executes on slider movement.
function slider1_Callback(hObject, eventdata, handles)
% hObject    handle to slider1 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Hints: get(hObject,'Value') returns position of slider
%        get(hObject,'Min') and get(hObject,'Max') to determine range of slider


% --- Executes during object creation, after setting all properties.
function slider1_CreateFcn(hObject, eventdata, handles)
% hObject    handle to slider1 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    empty - handles not created until after all CreateFcns called

% Hint: slider controls usually have a light gray background.
if isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
    set(hObject,'BackgroundColor',[.9 .9 .9]);
end

Example for external_prog

function k = myprog(i)
%prompt = 'Please enter a value to begin the count';
%i = input(prompt);
i = mydata;
while(i < 100000)
    clc
    i = i + 1;
    k = i + 2
    tic;
    toc;
end

Consider the above function is being called from a script like this

%scriptname: external_script
myprog

Now the problem is:

1. I will open the GUI
2. Press Start to start the script
3. Input number into text editor [Now the external function is running]
4. Press update to take the edit text data into the while loop of the external function. 

The step 4, how can I do that? How can I simply take the data to a while loop inside a function[Not on top of the function but inside the while loop while its being looped] upon pressing of pushbutton from GUI ?

anyone have an idea ? please share. Thanks.

EDIT: ****NOTE**** THE MY myprog ABOVE IS A DUMMY PROGRAM, NOT CONSIDERING myprog I JUST WANT TO "PASS THE DATA FROM GUI TEXT EDITOR INTO A WHILE LOOP INSIDE A EXTERNAL FUNCTION".

cppiscute
  • 707
  • 2
  • 10
  • 33
  • I don't understand how `mydata` influences `myprog` function call. Which test data are you talking about in the `while` loop? –  Feb 11 '16 at 13:53
  • As I said, the while loop is doing many things internally. All I am trying to do is to pass this "mydata" into the while loop. and this while loop is being positioned inside a function that is external to the GUI's .m file. – cppiscute Feb 11 '16 at 14:03
  • @CST-Link Sorry for the confusion, the while loop presented above is just for example here. The real while loop I have is really big and which calls ten's of calls to enternal function files to make calculations. – cppiscute Feb 11 '16 at 14:13

2 Answers2

0

You will want to alter the contents of myprog to check to see if the input is provided (using exist) and if not then prompt the user to enter the value.

function k = myprog(m)
    if ~exist('m', 'var')
        prompt = 'Please enter a value to begin the count';
        m = input(prompt);
    end 

    while(m < 100000)
        clc
        m = m + 1;
        k = m + 2
        tic;
        toc;
    end
end

Then your callback would simply be

function pb2_Callback(hObject, eventdata, handles)
    mydata = str2double(get(handles.edit1,'string'));
    myprog(mydata);
end

The other way is to break myprog into two functions. One that does the gathering of inputs from the user and another that actually does the processing on the data. You could then call the processing function from your GUI.

NOTE: Please don't use i or j as variables.

Community
  • 1
  • 1
Suever
  • 64,497
  • 14
  • 82
  • 101
  • Because `m` is a required function argument, it will always exist by the time the execution reaches the first `if` statement. –  Feb 11 '16 at 14:19
  • @CST-Link that is not true – sco1 Feb 11 '16 at 14:23
  • @CST-Link That's not true. It's not a required argument. If it's omitted, MATLAB won't complain until you try to use it. The `if` statement occurs before you try to use it. Please run the code before critiquing. The user can simply call `myprog()` and will get the prompt. – Suever Feb 11 '16 at 14:23
0

The main idea is to use the base workspace as variable-passing mechanism between the push button callback and the running script; write the variable with assignin

function pb2_Callback(hObject, eventdata, handles)
    assignin('base', 'mydata', str2double(get(handles.edit1,'string'));
end

and get the variable with evalin:

function k = myprog(i)
    while(i < 100000)
        clc;
        i = evalin('base', 'mydata') + 1;
        k = i + 2
        tic;
        toc;
    end
end

The variable mydata must be initialized before calling the myprog function. Also, the update pushbutton callback needs more effort in order to avoid unwanted values in the mydata variable.

Please note that, for this to work, you need to launch your GUI from your external script, and not the other way around. This is because the 'BusyAction' property of your push-button can be configured in only two ways: 1) to wait until the running callback is finished executing, or 2) to cancel its own execution if other callback is running. In both cases, if your script is run as a callback, you cannot influence it during its run from another callback.

  • Is funny how this answer was downvoted without any comment on why is downvoted... :-) –  Feb 11 '16 at 14:38
  • Thanks a ton, which is exactly what I was looking for. – cppiscute Feb 11 '16 at 14:39
  • @statisticalbeginner Glad that I could help. :-) –  Feb 11 '16 at 14:39
  • 1
    I not the guilty one here sorry, I did upvote and accepted your answer. There was a downvote form someone else before I upvoted. Thanks for the answer :) – cppiscute Feb 11 '16 at 14:40
  • I must mention one thing here, the start push button is starting the program(This program is a multi iteration one). Then I am intended to change the data with "Update" and when user presses update, this updated data will be used for immediate next iteration. BusyAction doesnt allow this? there is no workaround for this? Please leave a comment. Thanks – cppiscute Feb 11 '16 at 14:44
  • @statisticalbeginner I'm afraid not. To my knowledge is not possible to "pause" a running callback in order to execute another callback, then resume it. But I might be wrong... you can experiment to confirm or infirm this. Please note that this is valid only for callbacks, not for functions that are launched by command window. –  Feb 11 '16 at 14:49
  • The data from pushbutton2 updates while the pushbutton1 is still running, so it does work in my case(That is while inside a while loop). Please let me know if you happen to have a method to pass the handles of pushbutton to external program. In this case from GUI to "While loop". [I just want to check if the second push button is pressed when I am inside the while loop] – cppiscute Feb 11 '16 at 15:08
  • 1
    @statisticalbeginner Well, the simplest way is to set a specific `'Tag'` property to the push button, then use `findobj(gcf,'Tag','MySpecificTag')` in your external program to retrieve the handle of that button. –  Feb 11 '16 at 15:19