Code covered by the BSD License  

Highlights from
Event Builder

image thumbnail

Event Builder

by

 

10 Jul 2006 (Updated )

Easily schedule events to trigger a Stateflow chart

evbuilder(varargin)
function varargout = evbuilder(varargin)
% EVBUILDER M-file of the Event Builder
%      EVBUILDER, by itself, creates a new EVBUILDER or raises the existing
%      singleton*.
%
%      H = EVBUILDER returns the handle to a new EVBUILDER or the handle to
%      the existing singleton*.
%

% ENHANCEMENTS:
%  - information about eventtypes
%  - legend in axes
%  - im- and export signals
%  - check new events for conflicts
%  - implement "add event" and "remove event"

%DONE:
%  2006-07-03:  - add event in timeline
%               - implement scenarios
%               - implement zoom
%               - implement pan
%  2006-07-10:  - move eventline in timeline (to change time of event)
%  2006-07-13:  - remove y-axis tics
%               - tested in prerelease R2006b


% Edit the above text to modify the response to help evbuilder

%      EVBUILDER('CALLBACK',hObject,eventData,handles,...) calls the local
%      function named CALLBACK in EVBUILDER.M with the given input arguments.
%
%      EVBUILDER('Property','Value',...) creates a new EVBUILDER or raises the
%      existing singleton*.  Starting from the left, property value pairs are
%      applied to the GUI before evbuilder_OpeningFunction gets called.  An
%      unrecognized property name or invalid value makes property application
%      stop.  All inputs are passed to evbuilder_OpeningFcn via varargin.
%
%      *See GUI Options on GUIDE's Tools menu.  Choose "GUI allows only one
%      instance to run (singleton)".
%
% See also: GUIDE, GUIDATA, GUIHANDLES

% Last Modified by GUIDE v2.5 09-Jan-2007 15:19:52

% Begin initialization code - DO NOT EDIT
gui_Singleton = 1;
gui_State = struct('gui_Name',       mfilename, ...
    'gui_Singleton',  gui_Singleton, ...
    'gui_OpeningFcn', @evbuilder_OpeningFcn, ...
    'gui_OutputFcn',  @evbuilder_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 evbuilder is made visible.
function evbuilder_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 evbuilder (see VARARGIN)

% Choose default command line output for evbuilder
handles.output = hObject;
set(handles.EventBuilder_figure, 'color', [.8 .8 .8]);
%===
% obtain info from all charts in model
handles.info = EventBuilderCore('init');
% FROM EventBuilderCore
% % info.chartnames:                      chartnames       (cell)
% % info(i).(chartname).eventnames:       eventnames       (cell)
% % info(i).(chartname).eventtriggertype: eventtriggertype (cell)
% % info(i).(chartname).sampletime:       sampletime       (double)

% obtain all current data for Event Builder
events = get_param(gcb,'userdata');
if isempty(events)
    events = initevents;
end
handles.events = events;
% {'scenario',{'event','triggertype', [triggertimes]}, ts};

% store blockname of block that the evbuilder is linked to
handles.blockname = gcb;


%%==============update GUI===================
% update handles structure
guidata(hObject, handles);
% update uicontrols
updategui(handles);
% create toolbar
panzoombar(handles.EventBuilder_figure)

% UIWAIT makes evbuilder wait for user response (see UIRESUME)
% uiwait(handles.EventBuilder_figure);


% --- Outputs from this function are returned to the command line.
function varargout = evbuilder_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 selection change in popupScenarioSelection.
function popupScenarioSelection_Callback(hObject, eventdata, handles)
% hObject    handle to popupScenarioSelection (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Hints: contents = get(hObject,'String') returns popupScenarioSelection contents as cell array
%        contents{get(hObject,'Value')} returns selected item from popupScenarioSelection

updategui(handles);

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

% Hint: popupmenu 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 selection change in popupEventSelection.
function popupEventSelection_Callback(hObject, eventdata, handles)
% hObject    handle to popupEventSelection (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Hints: contents = get(hObject,'String') returns popupEventSelection contents as cell array
%        contents{get(hObject,'Value')} returns selected item from popupEventSelection

updategui(handles);


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

% Hint: popupmenu 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



function editEventTimes_Callback(hObject, eventdata, handles)
% hObject    handle to editEventTimes (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 editEventTimes as text
%        str2double(get(hObject,'String')) returns contents of editEventTimes as a double

i_event     = get(handles.popupEventSelection, 'value');
i_scenario  = get(handles.popupScenarioSelection, 'value');
events      = handles.events;
% {'scenario',{'event','triggertype', [triggertimes]}, ts};
enames      = {events{i_scenario,2}{:,1}};

newtt = get(hObject, 'String');

%transform to string
if iscell(newtt)
    newtt = strvcat(newtt{:});
end
%convert to numeric and store
if ~isempty(enames)
    % only add trigger time if events are specified
    events{i_scenario,2}{i_event,3} = sort(str2num(newtt));

    % store in handles structure
    handles.events = events;
    guidata(hObject, handles);
end
% update plot
updategui(handles)


% --- Executes during object creation, after setting all properties.
function editEventTimes_CreateFcn(hObject, eventdata, handles)
% hObject    handle to editEventTimes (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 button press in pushOK.
function pushOK_Callback(hObject, eventdata, handles)
% hObject    handle to pushOK (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
menuClose_Callback(hObject, eventdata, handles)

% --- Executes on button press in pushApply.
function pushApply_Callback(hObject, eventdata, handles)
% hObject    handle to pushApply (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)


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

% hObject can be pushOK as well!!

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


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

option = get(hObject,'Label');
if ~strcmp(option,'&Import')
    importevent(option, handles);
end



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


%% --------------------------------------------------------------------
function menuScheduleEvent_Callback(hObject, eventdata, handles)

% get eventname
sel_event = get(hObject, 'label');
% get timepoint
pos = get(handles.axesTimeLine,'CurrentPoint');
xpos = pos(1);


events = handles.events;
i_scenario  = get(handles.popupScenarioSelection, 'value');
i_event =  strmatch(sel_event, {events{i_scenario,2}{:,1}}, 'exact');
% current triggertimes
try
    tt = events{i_scenario,2}{i_event,3};
catch
    % no times set yet
    tt = [];
end
% {'scenario',{'event','triggertype', [triggertimes]}, ts};

newtt = [tt; xpos];


%convert to numeric and store
events{i_scenario,2}{i_event,3} = sort(newtt);

% store in handles structure
handles.events = events;
guidata(hObject, handles);
updategui(handles);

%% --------------------------------------------------------------------
function importevent(option, handles)

info   = handles.info;      %chart info
events = handles.events;    %events

%i_event     = get(handles.popupEventSelection, 'value');
i_scenario  = get(handles.popupScenarioSelection, 'value');

enames = {events{i_scenario,2}{:,1}};   %eventnames
snames = {events{:,1}};                  %scenarionames

no_events    = length(enames);
no_scenarios = length(snames);

for ii = 1:no_scenarios
    %loop over scenarios
    newevents = info.(option).eventnames;
    for jj = 1:length(newevents)
        %loop over new events
        %eventname
        events{ii,2}{no_events+jj,1} = newevents{jj};
        %triggertype
        events{ii,2}{no_events+jj,2} = info.(option).eventtriggertype{jj};
        %triggertimes
        events{ii,2}{no_events+jj,3} = [];
        %eventype - IS_REACTIVE
        events{ii,2}{no_events+jj,4} = 0;
        
        
    end
end



%update gui data
handles.events = events;
guidata(handles.EventBuilder_figure, handles);
%update gui
updategui(handles);


%% --------------------------------------------------------------------

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





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

handles.events = initevents;
guidata(hObject, handles);
updategui(handles);



function editEventTypes_Callback(hObject, eventdata, handles)
% hObject    handle to editEventTypes (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 editEventTypes as text
%        str2double(get(hObject,'String')) returns contents of editEventTypes as a double


% --- Executes during object creation, after setting all properties.
function editEventTypes_CreateFcn(hObject, eventdata, handles)
% hObject    handle to editEventTypes (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




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


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

i_scenario   = get(handles.popupScenarioSelection, 'value');
events       = handles.events;
no_scenarios = size(events, 1);

% copy current scenario
no_scenarios = no_scenarios+1;
events(no_scenarios,:) = events(i_scenario,:);

% rename new scenario
sname = events{i_scenario,1};
newname = [sname '1'];
%newname = inputdlg('enter new scenario name', 'Scenario Name', 1, {newname});
events{no_scenarios, 1} = newname;

% store data
handles.events = events;
guidata(hObject, handles);
updategui(handles);

% select new scenario
set(handles.popupScenarioSelection, 'Value', no_scenarios)
updategui(handles)

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

i_scenario   = get(handles.popupScenarioSelection, 'value');
events       = handles.events;

% rename scenario
sname = events{i_scenario,1};
newname = inputdlg('enter new scenario name', 'Scenario Name', 1, {sname});
if ~isempty(newname)
    events{i_scenario, 1} = newname{1};
end
% store data
handles.events = events;
guidata(hObject, handles);
updategui(handles);

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

i_scenario   = get(handles.popupScenarioSelection, 'value');
events       = handles.events;
no_scenarios = size(events, 1);

% delete current scenario
no_scenarios = no_scenarios-1;
events(i_scenario,:) = [];

% select new scenario if required
if i_scenario>no_scenarios
    set(handles.popupScenarioSelection, 'Value', no_scenarios)
end

% store data
handles.events = events;
guidata(hObject, handles);
updategui(handles);


%% --------------------------------------------------------------------
function events = initevents
%initialize the complete data array

%  {...
%   'scenarioname', ...
%   {'eventname', 'triggertype', [triggertimes], (Boolean)IS_REACTIVE}, ...
%   [sampletime_chart] ...
%  }

events = {'Scenario', {}, -1};



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

i_scenario   = get(handles.popupScenarioSelection, 'value');
events       = handles.events;
enames = {events{1,2}{:,1}};             %eventnames
snames = {events{:,1}};                  %scenarionames

no_events    = length(enames);
no_scenarios = length(snames);

current_event = get(handles.popupEventSelection, 'value');
if no_events<current_event
    return
end
eventname     = events{1,2}{current_event,1};
% remove from/add to right-click in axes
% edit-box enable/disable - do onot print values?

for ii = 1:no_scenarios
    events{ii,2}{current_event,4} = ~events{ii,2}{current_event,4};
end

%totalreactive = sum([events{1,2}{:,4}]);

if events{1,2}{current_event,4}
    %curent event is changed to REACTIVE
    % replace ground block with input signal
    blockname = replace_block(handles.blockname, ... 'Ground','Inport', ...
        'name', eventname,...
        'Inport', ...
        'noprompt');


else
    % current event is changed to NOT REACTIVE

    %set_param([handles.blockname '/Mux'], 'inputs', num2str(totalreactive) );
    blockname = replace_block(handles.blockname, ...'Inport','Ground', ...
        'name', eventname,...
        'Ground', ...
        'noprompt');



end



%update gui data
handles.events = events;
guidata(handles.EventBuilder_figure, handles);
%update gui
updategui(handles);


% --- Executes on button press in checkIsReactive.
function checkIsReactive_Callback(hObject, eventdata, handles)
% hObject    handle to checkIsReactive (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Hint: get(hObject,'Value') returns toggle state of checkIsReactive

menuChangeEventType_Callback(hObject, eventdata, handles)

Contact us