Thursday, June 29, 2017

[Matlab GUI] Set an absolute path for standalone application

When running a standalone application created by Matlab compiler, it needs to check if the code is running in deployed mode or Matlab mode because the path is changed depending on the mode.

Here is the code to check the running mode.

if isdeployed % Stand-alone mode.      
    [~, result] = system('path');
    currentpath = char(regexpi(result, 'Path=(.*?);', 'tokens', 'once'));
else % MATLAB mode.  
    currentpath = pwd;
end


Wednesday, June 28, 2017

[Matlab GUI] Create a shortcut key like Ctrl+P on the GUI windows created by GUIDE

Shortcut key can be set in the GUI created by GUIDE.

First, create the 'KeyPressFcn' callback function
Second, Using 'CurrentModifier' and 'eventdata.key', we can implement a shorcut key on the Matlab GUI. 'CurrentModifier' is used to detect if the buttons like Shift and Ctrl are pressed, and 'eventdata.Key' know which button is pressed.

-------------------------------------------------------

function mainGui_KeyPressFcn(hObject, eventdata, handles)
% hObject    handle to mainGui (see GCBO)
% eventdata  structure with the following fields (see MATLAB.UI.FIGURE)
% Key: name of the key that was pressed, in lower case
% Character: character interpretation of the key(s) that was pressed
% Modifier: name(s) of the modifier key(s) (i.e., control, shift) pressed
% handles    structure with handles and user data (see GUIDATA)

modifier = get(handles.mainGui,'CurrentModifier');
% can get the same string with get(handles.mainGui,'CurrentKey');
keyin = eventdata.Key;

if ~isempty(modifier)
    keypressed = sprintf('%s%s%s',modifier{1,1},'+',keyin);
else
    keypressed = keyin;
end

switch keypressed
    case 'f5'
        Run_Callback(hObject, eventdata, handles);
end      
----------------------------------------------------

Thursday, June 22, 2017

[Matlab GUI] Change the logo on the figure created by GUIDE

The Matlab logo displayed as default can be changed by using Java.

Insert the code below in the OpendingFcn.
------------------------------------------------------------------------
javaFrame = get(hObject,'JavaFrame');
javaFrame.setFigureIcon(javax.swing.ImageIcon(' image file'));
------------------------------------------------------------------------

Then, can see the new logo changed as below.

[Matlab GUI] Create the tab UI using uitabgroup and uitab in GUIDE

This code was tested on Matlab R2014b. I am not sure if it works in Matlab R2014a or earlier version.
Matlab R2014b and later version now support the commands for tab creation, i.e. can create tabs programmatically but not support yet for GUIDE.
So need some workaround for creating tabs in GUIDE.
Below is one of workaround for tab in GUIDE

1. OpeningFcn

handles = TabCreation(handles);

2. User-define function

% Create each tab into uitabgroup
function handles = TabCreation(handles)
% Prevent an annoying warning message
warning off MATLAB:uitabgroup:OldVersion

%---- Create a tab group
handles.tgroup = uitabgroup('Parent',handles.mainGui,'TabLocation','top');
% Set position [left, bottom, width, height], normalized unit
set(handles.tgroup,'Units','Normalized','Position',[0 0 1 0.945]);
% Create each tab
handles.maintab = uitab('Parent',handles.tgroup,'Title','Main');
handles.doetab = uitab('Parent',handles.tgroup,'Title','DoE (Automation)');
handles.simulinktab = uitab('Parent',handles.tgroup,'Title','Simulink');
% Place panels into each tab
set(handles.Tab1,'Parent',handles.maintab);
set(handles.Tab2,'Parent',handles.doetab);
set(handles.Tab3,'Parent',handles.simulinktab);

% Reposition each panel to the same location as Tab1
set(handles.Tab2,'Position',get(handles.Tab1,'Position'));
set(handles.Tab3,'Position',get(handles.Tab1,'Position'));

Related post
http://simulation4vehicle.blogspot.jp/2017/06/matlab-gui-create-tabs-using-uitabgroup.html


Monday, June 19, 2017

[Matlab] Send emails with Matlab

We can send emails using Matlab command via MS Outlook COM API.

First create a new function for COM API as below.(source from Matlab FileExchange)

-------------------------------------------------------------------------
function sendolmail(to,cc,subject,body,attachments)
%Sends email using MS Outlook. The format of the function is 
%Similar to the SENDMAIL command.
% Create object and set parameters.

% Without attachments, 
% sendolmail('email account','Title','Test message including');

h = actxserver('outlook.Application');
mail = h.CreateItem('olMail');
mail.Subject = subject;
mail.To = to;
mail.Cc = cc;
mail.BodyFormat = 'olFormatHTML';

% for multiline body
% ---
body = cellstr(body); % to get a vector of 1 lione string
mailbody = '';
for i=1:1:numel(body)
    mailbody = cat(2,mailbody, char(body(i)), '
');
end
% ------
mail.HTMLBody = mailbody;


% Add attachments, if specified.
if nargin == 5
    for i = 1:length(attachments)
        mail.attachments.Add(attachments{i});
    end
end
% Send message and release object.
mail.Send;
h.release;
---------------------------------------------------------------------------------

Second, type, for example, " sendolmail('email account','Mail from Matlab','Hello there'); " on the Matlab command or create a new m-file.
Then, we can send and receive an email as below.

-------------------
Sending an email through m script with the function above

to = 'a@email.com;b@email.com';%to = {'a@email.com';'b@email.com'};
cc = 'c@email.com';

title1 = 'Automation tool update ';
title2 = 'Ver 1';
title = sprintf('%s%s',title1,title2);

message ={'各位',...
    '',...
    'お疲れ様です',...
    '',...  
    'よろしくお願いします。'};

sendolmail(to,cc,title,message);
% for N=1:1:length(to)
%     sendolmail(to{N,1},title,message);
% end
----------

* See also the Matlab command "sendmail"