Copyright (c) 2015, 'name'
All rights reserved.
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are
met:
* Redistributions of source code must retain the above copyright
notice, this list of conditions and the following disclaimer.
* Redistributions in binary form must reproduce the above copyright
notice, this list of conditions and the following disclaimer in
the documentation and/or other materials provided with the distribution
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
POSSIBILITY OF SUCH DAMAGE.
System Design, Optimization, and Validation in a virtual environment
Tuesday, September 26, 2017
Monday, September 25, 2017
[Matlab GUI] Context Menu: right-click menu
Source URL
1. Programmatically
https://www.mathworks.com/help/matlab/ref/uicontextmenu.html
2. GUIDE
https://www.mathworks.com/help/matlab/creating_guis/creating-menus-in-a-guide-gui.html
1. Programmatically
https://www.mathworks.com/help/matlab/ref/uicontextmenu.html
2. GUIDE
https://www.mathworks.com/help/matlab/creating_guis/creating-menus-in-a-guide-gui.html
[Matlab GUI] Implementing drag and drop
1. Using Java Swing (Matlab FileExchange)
http://jp.mathworks.com/matlabcentral/fileexchange/53511-drag---drop-functionality-for-java-gui-components
2. Drag and drop of the graphic objects
https://www.mathworks.com/matlabcentral/answers/94681-how-do-i-implement-drag-and-drop-functionality-in-matlab
3. Stackoverflow
https://stackoverflow.com/questions/36833181/drag-and-drop-files-in-matlab-gui
Monday, August 14, 2017
[Matlab] Bode plot without Control Toolbox
When it comes to Bode plot, it is easy to draw a Bode plot with control toolbox, but Not everybody can get this toolbox.
For those who don't have Control Toolbox, let's see how to draw a Bode plot with only basic Matlab functions.
Assuming that a transfer function is as below.
1
-------
s + 1
Then, the magnitude and phase of the transfer function are neccsary for bode plot.
The magnitude and phase can be calculated by replacing s with jw.
where, j is the imaginary unit of complex number, and w is the frequency.
1 1
------- --> -----------
s + 1 j*w + 1
Possible matlab code for bode plot is as below.
--------------------------------------------------------
% Define frequency
freq = 0.01:0.01:1000;
% Transfer function
tf = 1 ./ (1 + i*freq);
% Magnitude
m = 20 * log10(abs(tf));
% Phase
phase = angle(tf);
% Plot
subplot(2,1,1)
semilogx(freq,m);
grid on
ylabel('Magnitude (dB)');
subplot(2,1,2)
semilogx(freq,phase);
grid on
ylabel('Phase (deg)');
xlabel('Frequency (rad/sec)');
--------------------------------------
As a result of the code above,
For those who don't have Control Toolbox, let's see how to draw a Bode plot with only basic Matlab functions.
Assuming that a transfer function is as below.
1
-------
s + 1
Then, the magnitude and phase of the transfer function are neccsary for bode plot.
The magnitude and phase can be calculated by replacing s with jw.
where, j is the imaginary unit of complex number, and w is the frequency.
1 1
------- --> -----------
s + 1 j*w + 1
--------------------------------------------------------
% Define frequency
freq = 0.01:0.01:1000;
% Transfer function
tf = 1 ./ (1 + i*freq);
% Magnitude
m = 20 * log10(abs(tf));
% Phase
phase = angle(tf);
% Plot
subplot(2,1,1)
semilogx(freq,m);
grid on
ylabel('Magnitude (dB)');
subplot(2,1,2)
semilogx(freq,phase);
grid on
ylabel('Phase (deg)');
xlabel('Frequency (rad/sec)');
--------------------------------------
As a result of the code above,
-----------------------------------------------------------------------
General method for Bode plot
% Define the numerator and denominator
num=[-0.1, -2.4, -181, -1950];
den = [1, 3.3, 990,2600];
tnum = [];
tden = [];
an = length(num);
bn = length(den);
% Numerator
for N = 1:1:an
if num(N) > 0
si ='+';
elseif num(N) <0 p="">0>
si = '';
end
temp = sprintf('%s%d%s%s%d ',si,num(N),'*','s.^',an-N);
tnum = [tnum temp];
end
% Denominator
for N = 1:1:length(den)
if den(N) > 0
si ='+';
elseif den(N) <0 p="">0>
si = '-';
end
temp = sprintf('%s%d%s%s%d ',si,den(N),'*','s.^',bn-N);
tden = [tden temp];
end
% Define a system transfer function
sys = @(s) eval(tnum)./ eval(tden);
% Define frequency as x
w=0.01:0.01:1000;
% Magnitude (dB)
amp=20*log10(abs(sys(w*1i)));
% Phase
ang=angle(sys(w*1i))*180/pi;
% Plot using subplot
subplot(211)
semilogx(w,amp);
ylabel('Magnitude (dB)')
grid on
subplot(212)
semilogx(w,ang);
xlabel('frequency (rad/sec)')
ylabel('phase (deg)')
grid on
Sunday, July 30, 2017
[Matlab] Plot the latitude and longitude information on Google Map
Source URL
1. https://www.mathworks.com/matlabcentral/fileexchange/27627-zoharby-plot-google-map
2. https://blogs.mathworks.com/pick/2012/05/04/plot-google-map/
Can download the function 'plot_google_map' from the source URL 1.
Example code 1.
lat = [48.8708 51.5188 41.9260 40.4312 52.523 37.982];
lon = [2.4131 -0.1300 12.4951 -3.6788 13.415 23.715];
plot(lon,lat,'.r','MarkerSize',20)
plot_google_map
Result 1
The result appears on new figure.
Example code 2
% load route data
load NatickToBOS
% plot route data
plot(Data001(:, 1), Data001(:, 2), 'r', 'LineWidth', 2);
line(Data001(1, 1), Data001(1, 2), 'Marker', 'o', ...
'Color', 'b', 'MarkerFaceColor', 'b', 'MarkerSize', 10);
line(Data001(end, 1), Data001(end, 2), 'Marker', 's', ...
'Color', 'b', 'MarkerFaceColor', 'b', 'MarkerSize', 10);
xlim([-71.4, -71]); axis equal off
% Google map
plot_google_map('maptype', 'roadmap');
zoomHandle = zoom;
set(zoomHandle, 'ActionPostCallback', @update_google_map);
1. https://www.mathworks.com/matlabcentral/fileexchange/27627-zoharby-plot-google-map
2. https://blogs.mathworks.com/pick/2012/05/04/plot-google-map/
Can download the function 'plot_google_map' from the source URL 1.
Example code 1.
lat = [48.8708 51.5188 41.9260 40.4312 52.523 37.982];
lon = [2.4131 -0.1300 12.4951 -3.6788 13.415 23.715];
plot(lon,lat,'.r','MarkerSize',20)
plot_google_map
Result 1
The result appears on new figure.
Example code 2
% load route data
load NatickToBOS
% plot route data
plot(Data001(:, 1), Data001(:, 2), 'r', 'LineWidth', 2);
line(Data001(1, 1), Data001(1, 2), 'Marker', 'o', ...
'Color', 'b', 'MarkerFaceColor', 'b', 'MarkerSize', 10);
line(Data001(end, 1), Data001(end, 2), 'Marker', 's', ...
'Color', 'b', 'MarkerFaceColor', 'b', 'MarkerSize', 10);
xlim([-71.4, -71]); axis equal off
% Google map
plot_google_map('maptype', 'roadmap');
zoomHandle = zoom;
set(zoomHandle, 'ActionPostCallback', @update_google_map);
Result 2
The result appears on new figure.
The result appears on new figure.
[Matlab GUI] Build simple Data Processing tool - 5/5
5. Clear the axes, and Move the figure to new figure (Undock)
After drawing a plot, you can either clear the plot or undock the plot from the axes.
1) Clear
function Clear_ClickedCallback(hObject, eventdata, handles)
% hObject handle to Clear (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
try
cla(all,'reset'); % Clear the entire axes and remove the y-axis
catch
cla();
end
handles.legend = [];
legend off;
set(handles.axes1,'Position',handles.inipos);
2) Undock
function Undock_ClickedCallback(hObject, eventdata, handles)
% hObject handle to Undock (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
warning('off');
fig_children = get(gcf,'children');
fig_Axes = findall(fig_children,'Type','Axes');
fig_legend = findall(fig_children,'Tag','legend');
fig = figure;
% copy an existing post from gui to new figure window
copyobj(fig_Axes,fig);
% Delete the legends because the size is not well-suited
dleg = findall(fig,'Tag','legend'); delete(dleg);
% Clear an offset in position of an existing post so it can be centered on a new figure window
set(gca,'ActivePositionProperty','outerposition');
set(gca,'Units','normalized');
set(gca,'OuterPosition',[0 0 1 1]);
set(gca,'position',[0.1200 0.1100 0.790 0.8150]);
% Change the default logo on the new figure appeared.
try
imgPath = fullfile(pwd,'itk.jpg');
javaFrame = get(handle(gcf),'JavaFrame');
javaFrame.setFigureIcon(javax.swing.ImageIcon(imgPath));
catch
if code == 1
warndlg('1','error');
elseif code == 2
warndlg('2','error')
end
end
% Set the legend on the new figure
l = legend('show'); set(l,'Interpreter','none');
warning('on');
Related Posts
0. Build simple plot tool
1. Create a tool layout in GUIDE
2. Load mat file in structure array, and Display the file list loaded on the listbox
3. Diplay variables of mat file, and Plot variables on the axes
4. How to add new plot to the existing plot, and Use dynamic legend
5. Clear the axes, and Move the figure to new figure (Undock)
After drawing a plot, you can either clear the plot or undock the plot from the axes.
1) Clear
function Clear_ClickedCallback(hObject, eventdata, handles)
% hObject handle to Clear (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
try
cla(all,'reset'); % Clear the entire axes and remove the y-axis
catch
cla();
end
handles.legend = [];
legend off;
set(handles.axes1,'Position',handles.inipos);
2) Undock
function Undock_ClickedCallback(hObject, eventdata, handles)
% hObject handle to Undock (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
warning('off');
fig_children = get(gcf,'children');
fig_Axes = findall(fig_children,'Type','Axes');
fig_legend = findall(fig_children,'Tag','legend');
fig = figure;
% copy an existing post from gui to new figure window
copyobj(fig_Axes,fig);
% Delete the legends because the size is not well-suited
dleg = findall(fig,'Tag','legend'); delete(dleg);
% Clear an offset in position of an existing post so it can be centered on a new figure window
set(gca,'ActivePositionProperty','outerposition');
set(gca,'Units','normalized');
set(gca,'OuterPosition',[0 0 1 1]);
set(gca,'position',[0.1200 0.1100 0.790 0.8150]);
% Change the default logo on the new figure appeared.
try
imgPath = fullfile(pwd,'itk.jpg');
javaFrame = get(handle(gcf),'JavaFrame');
javaFrame.setFigureIcon(javax.swing.ImageIcon(imgPath));
catch
if code == 1
warndlg('1','error');
elseif code == 2
warndlg('2','error')
end
end
% Set the legend on the new figure
l = legend('show'); set(l,'Interpreter','none');
warning('on');
Related Posts
0. Build simple plot tool
1. Create a tool layout in GUIDE
2. Load mat file in structure array, and Display the file list loaded on the listbox
3. Diplay variables of mat file, and Plot variables on the axes
4. How to add new plot to the existing plot, and Use dynamic legend
5. Clear the axes, and Move the figure to new figure (Undock)
[Matlab GUI] Build simple Data Processing tool - 4/5
4. How to add new plot to the existing plot, and Use dynamic legend
After selecting either one variable or two variables, we can both draw a plot and add the legend dynamically on the axes selected.
Example code
-----------------------------------------------------------
% Create a legend name
tleg = handles.legend(1:end-4);
yvar = get(handles.VariablesList,'String');
yvari = get(handles.VariablesList,'Value');
ytemp = yvar(yvari);
% Legend name: filename + variable name
leg = sprintf('%s%s%s',tleg,': ',ytemp{1});
% Get the data of y
result = handles.result;
y = result{yvari,1};
if isequal(checkp,1)% with x-axis variable selected
% Get the data of x
xn = get(handles.xVariables,'String');
xi = get(handles.xVariables,'Value'); xn=xn{xi,1};
x = result{xi,1};
% Plot and update the legend dynamically
pline = plot(handles.axes1,x,y,'DisplayName',leg);
hl = legend(handles.axes1,'-DynamicLegend');
xlabel(xn,'interpreter','none');
ylabel(ytemp{1},'interpreter','none');
else % Without x variable
% Plot and update the legend dynamically
pline = plot(handles.axes1,y,'DisplayName',leg);
hl = legend(handles.axes1,'-DynamicLegend');
ylabel(ytemp{1},'interpreter','none');
end
% Set the legend interpreter to none
set(hl,'Interpreter','none');
Related posts
0. Build simple plot tool.
1. Create a tool layout in GUIDE
2. Load mat file in structure array, and Display the file list loaded on the listbox
3. Display variables of mat file, and Plot variables on the axes
4. How to add new plot to the existing plot, and Use dynamic legend
5. Clear the axes, and Move the figure to new figure (Undock)
After selecting either one variable or two variables, we can both draw a plot and add the legend dynamically on the axes selected.
Example code
-----------------------------------------------------------
% Create a legend name
tleg = handles.legend(1:end-4);
yvar = get(handles.VariablesList,'String');
yvari = get(handles.VariablesList,'Value');
ytemp = yvar(yvari);
% Legend name: filename + variable name
leg = sprintf('%s%s%s',tleg,': ',ytemp{1});
% Get the data of y
result = handles.result;
y = result{yvari,1};
if isequal(checkp,1)% with x-axis variable selected
% Get the data of x
xn = get(handles.xVariables,'String');
xi = get(handles.xVariables,'Value'); xn=xn{xi,1};
x = result{xi,1};
% Plot and update the legend dynamically
pline = plot(handles.axes1,x,y,'DisplayName',leg);
hl = legend(handles.axes1,'-DynamicLegend');
xlabel(xn,'interpreter','none');
ylabel(ytemp{1},'interpreter','none');
else % Without x variable
% Plot and update the legend dynamically
pline = plot(handles.axes1,y,'DisplayName',leg);
hl = legend(handles.axes1,'-DynamicLegend');
ylabel(ytemp{1},'interpreter','none');
end
% Set the legend interpreter to none
set(hl,'Interpreter','none');
Related posts
0. Build simple plot tool.
1. Create a tool layout in GUIDE
2. Load mat file in structure array, and Display the file list loaded on the listbox
3. Display variables of mat file, and Plot variables on the axes
4. How to add new plot to the existing plot, and Use dynamic legend
5. Clear the axes, and Move the figure to new figure (Undock)
Subscribe to:
Posts (Atom)


