Entries tagged MATLAB

Add the data exploration buttons back to the figure toolbar in MATLAB R2018b or larer

Posted on Sep 13, 2019

In R2018b and later, the data exploration buttons (zoom in, zoom out, etc.) were moved from the figure toolbar to the axes toolbar. I use those buttons all the time, so this change is really annoying. What is relieving is that you can use the addToolbarExplorationButtons and removeToolbarExplorationButtons functions to bring the buttons back to the figure toolbar.

fig = figure;
surf(peaks);

addToolbarExplorationButtons(fig);     % add zoom buttons to the figure toolbar
removeToolbarExplorationButtons(fig);  % Remove the buttons from the figure toolbar

The functions work for the specified figure only. If you want to open all figures with the data exploration buttons, put the following code in the startup.m

set(groot,'defaultFigureCreateFcn',@(fig,~)addToolbarExplorationButtons(fig));
set(groot,'defaultAxesCreateFcn',@(ax,~)set(ax.Toolbar,'Visible','off'));

Vectorizing Value Assignment to an Array of Structures

Posted on Nov 7, 2015

MATLAB code:
% create an array of 100 structures
chan = struct('Index', num2cell(1:100));

% assign new numbers (larger by 3) to the Index field without using a loop

% Method 1
chan = arrayfun(@(s) setfield(s,'Index', s.Index + 3), chan);

% Method 2: This is faster than Method 1, but requires a temporary variable.
new_index = num2cell([chan.Index] + 3);
[chan.Index] = new_index{:};

% Method 3: Surprisingly using a loop is the fastest!!!
for m=1:100, chan(m).Index = chan(m).Index + 3; end

% The fastest time out of 10 runs on my desktop with R2015b
% Method 1: Elapsed time is 0.002056 seconds.
% Method 2: Elapsed time is 0.000357 seconds.
% Method 3: Elapsed time is 0.000124 seconds.

Another SQLite interface for MATLAB

Posted on Jun 17, 2015

This MEX package enables MATLAB to access SQLite databases via SQL queries. I wrote it because previous solutions did not do what I wanted in a simplistic way. The zip file below contains source code and 32-bit & 64-bit binaries for MS Windows.

Download: sqlite.zip (2.32MB)

The code was tested with SQLITE 3.11.0, MATLAB 2015b and Visual Studio Community 2015. You may need to install Visual C++ Redistributable Packages for Visual Studio 2015 from the Microsoft website to run the included MEX binaries. This program is distributed in the hope that it will be useful, but without any warranty.

Usage:
db = sqlite;  % create a class object
db.version    % SQLite version
db.connect('test.db');
db.query('CREATE TABLE Test (Subject TEXT, Score INTEGER);');
db.query('INSERT INTO Test (Subject,Score) VALUES (''English'',50);');
db.query('INSERT INTO Test (Subject,Score) VALUES (?,?);','Math',90); % bind parameter
db.query('UPDATE Test SET Score=80 WHERE Subject=''English'';');
table = db.query('SELECT * from Test;');
db.query('DELETE FROM Test WHERE Score<90;');

cell_mat = { uint8('A byte stream in a cell matrix is considered a blob.') };
db.query('CREATE TABLE BinaryLargeOBject (Object BLOB);');
db.query('INSERT INTO BinaryLargeOBject (Object) VALUES (?);',cell_mat); % handle blob

clear db;  % This also disconnect DB

How to execute daqregister(‘nidaq’) without running MATLAB as administrator

Posted on May 6, 2015

In order to control NI devices with Data Acquisition Toolbox in MATLAB, you first need to run MATLAB as administrator and execute the DAQREGISTER command. (See this.) However, running MATLAB as administrator is not possible, if you have to log in with a different username to be an administrator and your MATLAB activation type is Standalone Named User. MATLAB just won’t start when you switch to your admin account, because the Standalone Named User license does not allow anyone to use MATLAB but the licensed named user. You can bypass this problem by giving your non-admin MATLAB user write permission for the following two registry entries so that you can execute DAQREGISTER as a standard user. This was tested on 64-bit Windows 7 with MATLAB 2015a.

HKEY_LOCAL_MACHINE\SOFTWARE\Classes
HKEY_LOCAL_MACHINE\SOFTWARE\Wow6432Node\Classes

To change permissions for registry, you need to run REGEDIT as administrator. (Or open a command window as administrator and type REGEDIT.)

How to import TDT Tank into MATLAB

Posted on Oct 4, 2010

1. Introduction

Tucker-Davis Technologies (TDT) is a company supplying signal processing systems. The signals recorded by their System 3 are stored in a database called TTank and can be read out later. TDT provides application programming interfaces (APIs) for TTank access through ActiveX DLLs and, in any development environment where ActiveX can be incorporated, users can make TTank applications easily.

For example, in MATLAB, you can create a TTank object with the following command. (You need to install OpenDeveloper package before typing the command.)

TTX = actxcontrol('TTank.X');

Then you can call any API functions listed in the OpenDeveloper reference manual with the object. (Some code examples are installed with the OpenDeveloper package in C:\TDT\OpenEx\Examples\TTankX_Example\Matlab) If you do not have a TDT system and just want to read TTank, then you can try out TDT NeuroShare Kit. I have not tested it , but I guess it works in a similar way.

However, sometimes it is not convenient to use particular DLLs to access data, since it means that you are bound to the MS Windows platform and cannot read your data without the TDT software package. Considering that you never know how long you will be able to get necessary technical support from the vendor, it is probably not a bad idea to learn how to read the binary format of the TTank directly.

Continue…