function ws_evalmcw(editHndl);
%WS_EVALMCW Evaluates a list of functions in a editable text uicontrol.
%
% This is a modified version of the standard evalmcw function.
% The modification forces the command written in the edit text to operate in the base workspace in
% order to make use of variables already defined there. This avoids enclosing in the edit text
% variable creation commands for variables already defined in the base workspace.
% All command line outputs are redirected to the MCW. After an output line, a blank line is inserted.
% This function ignores the three leading characters '>> ' of the lines in the MCW, and automatically
% adds those characters at the begining of MCW command lines, to fully emulates the command window behaviour
% The content of the editable text uicontrol can be a cell array or a character array of strings
% and WS_EVALMCW will always return a cell array of strings.
%
% REMARKS: commands that affect the appearence of the command window (e.g. clc, more), do not have
% effect on the MCW.
%
%
% Author: Francesco di Pierro
% Ver: 2.1 & Date: 20/3/2002
%
% Force text in MiniCommand Window to be displayed immediately
figNumber=watchon;
drawnow;
str=get(editHndl,'String');
%force it to be an array of strings
if iscell(str)
str = char(str);
end
errorFlag=0;
numRows=size(str,1);
str_to_edit = {};
prompt = '>> ';
for count=1:numRows
try
%if the user have positionned the cursor on a new line prior to activating the callback
%a blank line is also passed to the eval function. If such a line is encountered, it gets skipped
if isempty(double(deblank(str(count,:))))
continue
end
%this creates the string to be executed in matlab base workspace
str_command = str(count,:);
%if the string to be executed starts with '>> ' (which is what this function writes
%in the MCW at the beginning of a line containing commands),
%ignore the first three characters
if strcmp('>> ',str_command(1:3))
str_command = str_command(4:end);
end
str_to_eval = ['out=evalc([''',str_command,''']);'];
%this evaluates the expression and capture matlab output in the variable out
evalin('base',str_to_eval);
%this assign to the local variable out variable the content of the base workspace varible out
out = evalin('base','out');
command = cellstr([prompt,str_command]);
%if the command produces outputs , skip a line
if not(isempty(out))
str_build = [command;cellstr(out)];
str_to_edit = [str_to_edit;str_build;{' '}];
%otherwise don't
else
str_build = command;
str_to_edit = [str_to_edit;str_build];
end
catch
errorFlag=1;
end
% Exit the loop as soon as there is an error
if errorFlag, break; end;
end;
% If an error has occurred, display the error in the MCW and reset the field.
% Otherwise, store the legal code in the object's UserData and direct the command line output in the MCW
if errorFlag,
set(editHndl,'ForegroundColor',[1 0 0]);
%Pad "lasterr" with spaces for consistency
lasterrStr=[32 lasterr];
errorStr=char({' Error Detected:'; lasterrStr; ' Resetting Input'});
set(editHndl,'String',errorStr);
pause(3);
oldStr=get(editHndl,'UserData');
set(editHndl,'String',oldStr);
set(editHndl,'ForegroundColor',[0 0 0]);
else
set(editHndl,'String',str_to_edit);
set(editHndl,'UserData',str);
end;
%clear the temporary variable out in the base workspace
evalin('base','clear out');
watchoff(figNumber);