Sunday, March 26, 2017

[Matlab GUI] Create a hierarchical tree node and get a node name selected

In Matlab GUI, a hierarchical tree node can be created using the built-in function "uitree"and "uitreenode".

---- Source URL: http://undocumentedmatlab.com/blog/uitree
The uitree is based on an underlying Java component, and accepts an optional figure handles followed y P-V (property-value) pairs. The uitree properties are Root, ExpandFcn, SelectionChangeFcn, Position (also Parent). A "v0" input argument are necessary to suppress a warning message.
Note that uitree is always created as a direct child of the containing figure, ignoring creation-time Parent values.
--------------------------------------------------------------------------------------

The following example shows how to create a uitree and get a selected node name using Matlab GUIDE.
Download example files: uitreeCreate_DisplayName.fig and uitreeCreate_DisplayName.m

1. Create a GUIDE template

























2. Create a uitree in the OpeningFcn function

% --- Executes just before uitreeCreate_DisplayName is made visible.
function uitreeCreate_DisplayName_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 uitreeCreate_DisplayName (see VARARGIN)

% Create a hierarchical tree for Fruits and Vegetables
% Fruits
fruits = uitreenode('v0', 'Fruits', 'Fruits', [], false);
fruits.add(uitreenode('v0', 'Apple',  'Apple',  [], true));
fruits.add(uitreenode('v0', 'Pear',   'Pear',   [], true));
fruits.add(uitreenode('v0', 'Banana', 'Banana', [], true));
fruits.add(uitreenode('v0', 'Orange', 'Orange', [], true));
% Vegetables
veggies = uitreenode('v0', 'Veggies', 'Vegetables', [], false);
veggies.add(uitreenode('v0', 'Potato', 'Potato', [], true));
veggies.add(uitreenode('v0', 'Tomato', 'Tomato', [], true));
veggies.add(uitreenode('v0', 'Carrot', 'Carrot', [], true));
% Root node
root = uitreenode('v0', 'Food', 'Food', [], false);
root.add(veggies);
root.add(fruits);

% Create a uitree on a specified GUIDE figure
mtree = uitree('v0',handles.figure1, 'Root', root);
% Specify the size of a uitree handles
set(mtree,'pos',[30,30,200,300]);
% Save the handle "mtree" to global handle structure
handles.mtree = mtree;
% Choose default command line output for uitreeCreate_DisplayName
handles.output = hObject;
% Update handles structure
guidata(hObject, handles);

3. Display a selected node name on the edit
After clicking the pushbutton, a selected node name will be displayed on the edit
Therefore, the function to display a selected node name will be created in the pushbutton callback function.

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

% Load the mtree handle
mtree = handles.mtree;
% a=mtree.getSelectedNodes;
nodename=mtree.SelectedNodes;
nName = nodename(1);

%value =nName.getValue(); % Currently, may ignore this.
% Get a selected node name
name = char(nName.getName());
% Get a parent name of a selected one
parentnode = nName.getParent();
parentname = char(parentnode.getName());

modelpath = [parentname,'/',name];
% Display a selected node and parent name on the edit
set(handles.edit1,'String',modelpath);

4. Result







Friday, March 24, 2017

[Matlab / Simulink] Initialize Simulink parameters

In the model properites of Simulink model, parameters defined in an m script can be loaded using pre-load function shown as below. 
In case paramter file is placed in the different folder where simulink model is not placed, path setting has to be changed.

* Model pre-load fuction
% Get a model path
[pathstr,~,~]=fileparts(get_param(gcs,'filename'));
% Create a parameter file path
path=sprintf('%s%s',pathstr,'\ParameterChanger');
cd(path);
% Load a parameter m script
testp;
cd ..


Sunday, March 12, 2017

[Filter] Analog filter to Digital filter

Analog filter can be converted to digital filter (IIR Filter) by bilinear transformation.
















Bilinear transformation, Pre-warping to compensate cut-off frequency

Download

---------------------------
Design butterworth filter using Matlab function 'butter' (Signal Processing Toolbox)
To use this, signal processing toolbox license is necessary.

[b, a] = butter(order, W, filter type)

# Filter type
1. LowPass Filter = 'low'
2. HighPass Filter = 'high'
3. Bandstop Filter = 'bandstop'
4. Bandpass Filter = 'bandpass'

For example, 6th order lowpass filter
fc = 300; % cut-off frequency
fs = 1000; % sampled data (sampling frequency)

[b,a] = butter(6,fc/(fs/2));
freqz(b,a)



[Matlab] Insert values in specified blocks / Extract signal list from a bus select block

1. Extract signal list selected

m script
---------------------------------------------------------------------
% Define parameter to be used
var=get_param(gcb,'OutputSignals');

% Find the comma
com = strfind(var,',');
[~,n] = size(com);
sig = cell(n,1);

% Retrieve each signal from strings
for N=1:1:n+1  
    if N == 1;
        sig{N,1} = var(1:com(1)-1);
    elseif N > 1 && N         sig{N,1} = var(com(N-1)+1:com(N)-1);
    else
        sig{N,1} = var(com(N-1)+1:end);
    end
end
---------------------------------------------------------------------

2. Automatically input values in specified blocks(Constant block)

m script
---------------------------------------------------------------------
% Model path
mpath = '... / ... /...';
% subsystem path
spath =  '... / ... /...';
% Block name
bname = { '...', '...', ... '...'};
% Define values to change
val = [1, 2, ..., n];

% size
[n, ~] = size(val);
% Implementation
try
   for N = 1:1:n
      fpath = sprintf('%s%s%s',spath,bname,val{N,1});
      set_param(fpath,'Value',num2str(val(N,1)));
   end
catch
   disp(fpath); % Display a path and block name failed to enter
end











[Matlab GUI] Create the tab UI using GUIDE

This example shows how to create tab.

M script
----------------------------------------------------------------
% Prepare a tab-group consisting of two tabs
hTabGroup = uitabgroup; drawnow;
% Define tab position
% Set(hTabGroup,'Position',[.1 .2 .5 .3]);
set(hTabGroup,'Position',[.1 .2 .8 .5]);

% Set two tabs "Panel 1" and "Panel 2" as child 
tab1 = uitab(hTabGroup, 'title','Panel 1');
a = axes('parent', tab1); surf(peaks);
tab2 = uitab(hTabGroup, 'title','Panel 2');
uicontrol(tab2, 'String','Close', 'Callback','close(gcbf)');
h2=uitabgroup('parent',tab2); %drawnow;

% Set the tab "Panel 2" as parent
set(h2,'Position',[.2 .3 .3 .2]);
% Set two tabs "t1" and "t2" as child of "Panel 2"
t1 = uitab(h2,'title','t1');
t2 = uitab(h2,'title','t2');
----------------------------------------------------------------

Download "guide_tab.fig" and "guide_tab.m"

[CarSim / TruckSim] CarSim/TruckSim Automation Tool

1. Objective
 Automate CarSim/TruckSim simulations including Simulink models

2. Overview
 2.1 Load all dataset of CarSim/TruckSim
 2.2 Select datasets to run and create lots of dataset list to run
 2.3 After setting datasets, run all combinations of datasets defined in the tool.
 2.4 Most parameters used in CarSim/TruckSim and all variables specified in Simulink model are controlled by this tool.

It was created using COM API of CarSim / TruckSim on Matlab R2014b.
Only run on the Matlab environment.

Related download link
Download link
1. First draft version (Aug. 2016)
2. Second version (Sep. 2017)
 2.1 Three windows have been integrated into one window with 3 tabs.
 2.2 Save and Load DoE configurations to/from Excel.
 2.3 New load button for ADAS scenario parameters has been added.
 2.4 Each scenario name defined in Excel is available as filename when saving the output variables.

Sunday, March 5, 2017

[Matlab GUI] Insert a logo image on matlab figure

% Load an image file
imagefile = 'image.jpg';
% load an image file
im=imread(imagefile);
clf
plot(randn(1,100))
% Set an image position
axes('position',[0,0.9,0.1,0.1])
% Display an image on the figure
imshow(im)

As a result, it looks as below.