Matlab Computation Time for Synonyms - matlab

I am importing wordnet library in matlab code given below but it takes almost 5-10 seconds for giving me synonyms of each word.
can its time be reduced in some way because I have to test it on numerous words
Thanks
Code:
function [status,varargout] = dictionary(text)
%DICTIONARY checks the spelling status of word(s) and returns synonyms if found.
%
% [status,synonyms] = dictionary(text);
% status = dictionary(text);
%
% text: word(s) separated by a single space.
% status: returns '1' if word(s) exist in dictionary or '0' otherwise.
% synonyms: an array of synonyms of word(s) or otherwise returns
% a message 'No Synonyms Found!' or 'Incorrect Spelling!'.
%
% Examples:
% [status,synonyms] = dictionary('peddler');
% [status,synonyms] = dictionary('walk match ground');
% status = dictionary('hysteria');
%
% Separating string of words into arrays of words.
k=1;
temp='';
% t=fopen('Crime.txt');
% text=textscan(t,'%s');
% text=texts{1,1};
% fclose(t);
%text='ignorevbnnn';
for n=1:length(text);
if ~isspace(text(n))
temp = [temp text(n)];
else
if ~isspace(text(n-1))
words{k} = temp;
end
temp='';
k=k+1;
end
end
words{k} = temp;
% Opening MS Word and Starting the Spelling Check & Finding Synonyms
Doc = actxserver('Word.Application');
m=1;
for n=1:length(words)
if ~isempty(words{n})
status(m) = invoke(Doc,'CheckSpelling',words{n},[],1,1);
if nargout==2
X = invoke(Doc,'SynonymInfo',words{n});
Synonyms = get(X,'MeaningList');
Meanings{m,1} = words{n};
if length(Synonyms)==0 & status(m)==1
Meanings{m,2} = 'No Synonyms Found!';
elseif status(m)==0
Meanings{m,2} = 'Incorrect Spelling!';
else
for k=2:length(Synonyms)+1
Meanings{m,k} = Synonyms{k-1};
end
end
end
m=m+1;
end
end
if exist('Meanings','var')
varargout = {Meanings};
end
status = all(status);
invoke(Doc,'Quit');
delete(Doc);

Related

Using KbQueueCheck to continuously check for device input

I am using PsychToolbox to run an experiment on a fMRI scanner. I want to collect the timepoints of pulses sent out by the scanner. If you are not familiar with this topic: A scanner gives out a signal - equivalent to pressing keyboard digit "5" - in defined time-intervals. The first such signal starts the code, such that measurements by the scanner, and starting time of the code are synchronized. My code is sequential, as sketched out below. How could I implement a loop which at any time checks for these inputed "5", even though my main code runs in a "for"-loop?
My code:
% here I wait for the scanner to input a "5", then my code will start
KbQueueCreate;
KbQueueStart;
Trigger = KbName('5%');
keyboard = -1;
AllowedKeys = zeros(256,1); %Disable all keys
AllowedKeys([LeftPress RightPress MiddlePress]) = 1; %Also allow escape always.
while 1
[pressed, firstPress] = KbQueueCheck();
if firstPress(Trigger)
startExperiment = GetSecs;
break
end
end
KbQueueStop;
KbQueueRelease;
% In the foolowing loop, the main experiment runs. here I show a screen,
% signal etc.
for i = 1:nrTrials
% here I would like to have code checking for scanner inputs
end
The function KbQueueCheck should check for any keyboard input starting from the most recent call to KbQueueStart.
Any input is very welcome.
I think you're going about this all wrong. There are much simpler ways to capture and process events without involving expensive for loops at all. Specifically, matlab (and octave) do this most effectively via graphics handle instances (even though you don't necessarily need a graphics object to be visible for it).
Here's a very simple example (I created this on octave, but it should also work on matlab). Run it to see how it works, then study and modify accordingly.
%% In file 'run_fMRI_experiment.m'
% Main experiment function
function fMRIData = run_fMRI_experiment
global fMRIData;
F = figure;
text(-1,0,{'Glorious Experiment, she is now runnink, yes?', 'Please to be pressink "5", I will collectink data.', '', 'Please to be pressink key of escapeness when finishedski, da?'}, 'fontsize', 14);
axis off; axis ([-1, 1, -1, 1]);
set (F, 'keypressfcn', #detectFirstKeyPress);
waitfor(F);
end
% subfunctions (i.e. in same file) acting as callback functions
function detectFirstKeyPress(CallerHandle, KeyPressEvent)
if strcmp(KeyPressEvent.Key, '5')
set (CallerHandle, 'keypressfcn', {#detectKeyPresses, tic()});
fprintf ('Experiment started at %s\n', datestr(now()));
end
end
function detectKeyPresses (CallerHandle, KeyPressEvent, StartTime)
if strcmp (KeyPressEvent.Key, '5');
global fMRIData; fMRIData(end+1) = toc(StartTime);;
fprintf('"5" pressed at %d seconds.\n', fMRIData(end)); return
elseif strcmp (KeyPressEvent.Key, 'escape');
disp ('Escape Pressed. Ending Experiment');
close (CallerHandle);
end
end
PS: Note that for a keypress event to be considered linked to the figure, the figure must be in focus. I.e. do not press '5' while the terminal is in focus, this won't be counted. Just launch the function and start pressing buttons as the window appears.
PS2: As an aside, if you must perform certain tasks asynchronously, matlab provides the batch function for this. Also worth having a look.
We do the similar stuff with our scanner, so I providing a function I use. I normally only detect first trigger to start the stimulus, and won't read later triggers. But I do register all triggers, as well as other response, in the background, so I can check the events in case there is any problem.
The function, KbQueue.m, can do almost all related response collection, including background key register. Please read the help for how to use different functionality. Some part of the code, such as different keyboard indices, is not well tested for MAC and Linux.
function varargout = KbQueue(cmd, param)
% KbQueueXXX series functions have some good features:
% 1. detect short key event, like those from fOPR KbCheck may miss;
% 2. detect only interested keys;
% 3. buffer key event while code is running.
%
% But the syntax of KbQueueXXX is a little inconvenient due to the flexibility.
%
% This code tries to provide a convenient wrapper for practical purpose, by
% sacrificing some flexibility, like deviceIndex etc. By default, KbQueue
% detects events for the first device (this doesn't seem the case for Windows,
% where it detects all devices). If you have more than one keyboard, and like to
% detect a different one, you have to add following in your code:
%
% global KbQueueDevice; KbQueueDevice = 2;
%
% where 2 means the second device. You need to find this index for your target
% keyboard.
%
% KbQueue('start', keys);
% - Create a queue and start it. The second input specify the keys to be queued.
% The default are numbers 1~5, both keyboard and keypad numbers. The key names
% adopt those with KbName('UnifyKeyNames'). The 'start' command can be called
% more than once to update new keys. If it is not called, other subfunctions
% will call it with the default keys automatically.
%
% Example:
% KbQueue('start', {'LeftArrow' 'RightArrow'}); % start to queue 2 arrows
%
% The input keys can also be 256-element keyCode, such as that returned by
% KbCheck etc. It can also be the index in the keyCode, such as that returned by
% KbQueue('wait') etc.
%
% nEvents = KbQueue('nEvents' [, 'type']);
% - Return number of events in the queue. The default event type is 'press',
% which returns the number of keypress. It can be 'release' or 'all', which will
% return number of release events or all events.
%
% [pressTime, pressCode] = KbQueue('wait' [, secs or keys]);
% - Wait for a new event, and return key press time and keycode. During the
% wait, pressing Escape will abort code execution, unless 'Escape' is included
% in defined keys.
%
% If there is no second input, this will wait forever till a defined key by
% previous 'start' command is detected.
%
% If the second input is numeric, it will be interpreted as the seconds to wait,
% and wait for a key defined by previous 'start' command is pressed, or the
% seconds has elapsed, whichever is earlier. If there is no any event during the
% time window, both output will be empty. For example:
%
% [t, key] = KbQueue('wait', 1); % wait for 1 sec or a key is detected
%
% If the second input is a string or cellstr, it will be interpreted as the
% key(s) to detect. The provided keys here affects only this call, i.e. it has
% no effect on the queue keys defined by previous 'start' command. For example:
%
% t = KbQueue('wait', {'5%' '5'}); % wait till 5 is pressed
%
% [pressCodeTime, releaseCodeTime] = KbQueue('check' [, t0]);
% - Return first and last press keycode and times for each queued key, and
% optionally release keycode and times. The output will be empty if there is no
% buffered response. Both output are two row vector, with the first row for the
% keycode and second row for times. If the second input t0, default 0, is
% provided, the returned times will be relative to t0. For example:
%
% press = KbQueue('check'); % return 1st and last key press in the queue
% pressedKey = KbName(press(1, :); % convert keycode into key names
%
% KbQueue('flush');
% - Flush events in the current queue.
%
% t = KbQueue('until', whenSecs);
% - This is the same as WaitSecs('UntilTime', whenSecs), but allows to exit by
% pressing ESC. If whenSecs is not provided or is already passed, this still
% checks ESC, so allows use to quit. For example:
% KbQueue('until', GetSecs+5); % wait for 5 secs from now
% KbQueue('until'); % only check ESC exit
%
% [pressCodeTime, releaseCodeTime] = KbQueue('stop' [, t0]);
% - This is similar to 'check' command, but it returns all queued events since
% last 'flush', or since the queue was started. It also stops and releases the
% queue. This provides a way to check response in the end of a session. For
% example:
% KbQueue('start', {'5%' '5'}); % start to queue 5 at beginning of a session
% KbQueue('flush'); % optionally remove unwanted events at a time point
% t0 = GetSecs; % the start time of your experiment
% % run your experiment
% pressCodeTime = KbQueue('stop', t0); % get all keycode and time
% 10/2012 wrote it, xiangrui.li#gmail.com
% 12/2012 try to release queue from GetChar etc, add nEvents
% 11/2014 try to use response device for OSX and Linux
persistent kCode started evts;
global KbQueueDevice; % allow to change in user code
if isempty(started), started = false; end
if nargin<1 || isempty(cmd), cmd = 'start'; end
if any(cmd=='?'), subFuncHelp('KbQueue', cmd); return; end
if strcmpi(cmd, 'start')
if started, BufferEvents; end
if nargin<2
param = {'1' '2' '3' '4' '5' '1!' '2#' '3#' '4$' '5%'};
end
KbName('UnifyKeyNames');
if ischar(param) || iscellstr(param) % key names
kCode = zeros(256, 1);
kCode(KbName(param)) = 1;
elseif length(param)==256 % full keycode
kCode = param;
else
kCode = zeros(256, 1);
kCode(param) = 1;
end
if isempty(KbQueueDevice), KbQueueDevice = responseDevice; end
try KbQueueReserve(2, 1, KbQueueDevice); end %#ok
KbQueueCreate(KbQueueDevice, kCode);
KbQueueStart(KbQueueDevice);
started = true;
return;
end
if ~started, KbQueue('start'); end
if strcmpi(cmd, 'nEvents')
BufferEvents;
n = length(evts);
if n, nPress = sum([evts.Pressed] == 1);
else nPress = 0;
end
if nargin<2, param = 'press'; end
if strncmpi(param, 'press', 5)
varargout{1} = nPress;
elseif strncmpi(param, 'release', 7)
varargout{1} = n - nPress;
else
varargout{1} = n;
end
elseif strcmpi(cmd, 'check')
[down, p1, r1, p2, r2] = KbQueueCheck(KbQueueDevice);
if ~down
varargout = repmat({[]}, 1, nargout);
return;
end
if nargin<2, param = 0; end
i1 = find(p1); i2 = find(p2);
varargout{1} = [i1 i2; [p1(i1) p2(i2)]-param];
if nargout>1
i1 = find(r1); i2 = find(r2);
varargout{2} = [i1 i2; [r1(i1) r2(i2)]-param];
end
elseif strcmpi(cmd, 'wait')
endSecs = GetSecs;
secs = inf; % wait forever unless secs provided
newCode = kCode; % use old keys unless new keys provided
if nargin>1 % new keys or secs provided
if isempty(param), param = inf; end
if isnumeric(param) % input is secs
secs = param;
else % input is keys
newCode = zeros(256, 1);
newCode(KbName(param)) = 1;
end
end
esc = KbName('Escape');
escExit = ~newCode(esc);
newCode(esc) = 1;
changed = any(newCode~=kCode);
if changed % change it so we detect new keys
BufferEvents;
KbQueueCreate(KbQueueDevice, newCode);
KbQueueStart(KbQueueDevice); % Create and Start are twins here :)
else
KbQueueFlush(KbQueueDevice, 1); % flush KbQueueCheck buffer
end
endSecs = endSecs+secs;
while 1
[down, p1] = KbQueueCheck(KbQueueDevice);
if down || GetSecs>endSecs, break; end
WaitSecs('YieldSecs', 0.005);
end
if changed % restore original keys if it is changed
BufferEvents;
KbQueueCreate(KbQueueDevice, kCode);
KbQueueStart(KbQueueDevice);
end
if isempty(p1)
varargout = repmat({[]}, 1, nargout);
return;
end
ind = find(p1);
if escExit && any(ind==esc)
error('User pressed ESC. Exiting ...');
end
varargout = {p1(ind) ind};
elseif strcmpi(cmd, 'flush')
KbQueueFlush(KbQueueDevice, 3); % flush both buffers
evts = [];
elseif strcmpi(cmd, 'until')
if nargin<2 || isempty(param), param = 0; end
while 1
[down, t, kc] = KbCheck(-1);
if down && kc(KbName('Escape'))
error('User pressed ESC. Exiting ...');
end
if t>=param, break; end
WaitSecs('YieldSecs', 0.005);
end
if nargout, varargout = {t}; end
elseif strcmpi(cmd, 'stop')
KbQueueStop(KbQueueDevice);
started = false;
if nargout
BufferEvents;
if isempty(evts)
varargout = repmat({[]}, 1, nargout);
return;
end
isPress = [evts.Pressed] == 1;
if nargin<2, param = 0; end
varargout{1} = [[evts(isPress).Keycode]
[evts(isPress).Time]-param];
if nargout>1
varargout{2} = [[evts(~isPress).Keycode]
[evts(~isPress).Time]-param];
end
end
KbQueueRelease(KbQueueDevice);
else
error('Unknown command: %s.', cmd);
end
function BufferEvents % buffer events so we don't lose them
n = KbEventAvail(KbQueueDevice);
if n<1, return; end
for ic = 1:n
foo(ic) = KbEventGet(KbQueueDevice); %#ok
end
if isempty(evts), evts = foo;
else evts = [evts foo];
end
end
end
function idx = responseDevice
if IsWin, idx = []; return; end % all keyboards
clear PsychHID; % refresh
[ind, pName] = GetKeyboardIndices;
if IsOSX
idx = ind(1); % based on limited computers
else % Linux
for i = length(ind):-1:1
if ~isempty(strfind(pName{i}, 'HIDKeys')) || ...
~isempty(strfind(pName{i}, 'fORP')) % faked, need to update
idx = ind(i);
return;
end
idx = ind(end); % based on limited computers
end
end
end

Splitting a wav file in Matlab

I previously asked a question, which is related to my actual problem. I converted a mp3 file to a wav file, by using the program audacity. Th duration of the wav-file is about 10 seconds. I want to split it in 10 parts, which means, each part takes 1 second. How can i make it happen? Please apologize my ignorance, even though the community provided me some answer.
[y,fs]=wavread('UnchainMyHeart');
t=linspace(0,length(y)/fs,length(y));
plot(t,y)
The code here, shows my wav file in the time domain, but you probably can't see it, since you don't have the wav file. Doesn't matter. Now, is it possibly to continue from there, concering my question? I believe it has somthing to do with matrix...
Unfortunatley I lack the knowledge, therefore I always appreciate your help!
It depends on what you want to do next. The easiest way to get part of signal is:
part=y(1:Fs);
you probaly want array of this things so use FOR, or WHILE sructure, smth like this:
%in loop
part(i)=(i*Fs:i*Fs+Fs);
i=i+1;
be aware of array length
Here is a WavReader.m class which reads wav in chunks of the same length.
To read 1-second chunks, use it like this:
wr = WavReader('c:/wavs/killyourmama.wav');
wr.SetChunkLengthInMiliSeconds( 1000.0 );
while(true)
[pcm, num_read] = wr.ReadChunk();
if( num_read==0 ); break; end;
fprintf('Progress: %5.2f%%\n', wr.Progress() );
end
Here is the class:
classdef WavReader < handle
%WavReader Reads wav files in chunks
% Flexible wav chunk reader.
%
% 1) Usage steps in case of full file read:
%
% Create wav reader:
% wr = WavReader(path);
% Read all samples:
% [samples, count] = GetAllSamples();
%
% 2) Usage steps in case of range read:
%
% Create wav reader:
% wr = WavReader(path);
%
% Read samples from within a range:
% [samples, count] = GetSamples(seconds_start, seconds_finish);
%
% 3) Usage steps in case chunk-by-chunk read:
%
% Create wav reader:
% wr = WavReader(path);
%
% Set range of interest:
% wr.SetRange( seconds_start, seconds_finish );
%
% Set chunk size you want to read:
% wr.SetChunkLength( chunk_size );
%
% Read chunk-by-chunk:
% while(num_read)
% [pcm, num_read] = wr.ReadChunk();
%
%
properties (Access=private)
% Info
path;
fs;
total_samples;
total_seconds;
% Range of interest
range_length;
range_samples;
% Current state of chunks
chunk_length;
total_chunks;
chunks_read;
end
methods
function obj = WavReader( in_path_wav )
% Constructor
try
obj.path = in_path_wav;
info = audioinfo(obj.path);
obj.fs = info.SampleRate;
obj.total_samples = info.TotalSamples;
obj.total_seconds = info.Duration;
catch
error('Problem with opening wav file. Exiting.')
end
% Defaul reads full range, whole file.
obj.SetRangeFull();
end
%%
function [range_length] = SetRangeFull(obj)
%SetRangeFull Sets the range to full (we are interested in all samples).
try
obj.range_samples = audioread(obj.path);
catch
error('Problem with reading wav file. Exiting.')
end
obj.range_length = obj.total_samples;
range_length = obj.range_length;
end
%%
function [range_length] = SetRange(obj, start_sec, end_sec)
% SetRange Sets target range of interest.
%
%# [range_length] = SetRange(obj, start_sec, end_sec)
%# INPUT start_sec: staring point in target range
%# INPUT end_sec: ending point in target range
%# OUTPUT range_length: number of total samples available in the range
%
% To use full range call SetRangeFull().
% If start beyond end: throws exception.
% If end beyond end: sets end to end.
[start_sec, end_sec] = obj.checks( start_sec,end_sec );
% Full range if both zeroes:
%if(start_sec==0 && end_sec==0)
% start_sample = 1;
% stop_sample = obj.total_samples;
%else
start_sample = obj.seconds_to_samples(start_sec);
stop_sample = obj.seconds_to_samples(end_sec);
%end
obj.range_length = stop_sample - start_sample;
obj.range_samples = 0;
try
obj.range_samples = audioread(obj.path, [start_sample, stop_sample]);
catch
error('Problem with reading wav file. Exiting.')
end
range_length = obj.range_length;
end
%%
function [total_chunks] = SetChunkLength( obj, chunk_length )
% SetChunkLength Defines chunk length for chunk-by-chunk read.
%
%# [total_chunks] = SetChunkLength( obj, chunk_length )
%# INPUT chunk_length: desired chunk length
%# OUTPUT total_chunks: total number of available full chunks
%
% Abandonds last incomplete chunk.
obj.chunk_length = chunk_length;
obj.chunks_read = 0;
obj.total_chunks = round( obj.range_length / obj.chunk_length - 0.5 );
total_chunks = obj.total_chunks;
end
%%
function [total_chunks] = SetChunkLengthInMiliSeconds( obj, chunk_length_ms )
% SetChunkLengthInMiliSeconds Defines chunk length for chunk-by-chunk read.
%
%# [total_chunks] = SetChunkLengthInMiliSeconds( obj, chunk_length_ms )
%# INPUT chunk_length_ms: desired chunk length in mili-seconds
%# OUTPUT total_chunks: total number of available full chunks
%
% Abandonds last incomplete chunk.
seconds = chunk_length_ms/1000.0;
chunk_length_samples = seconds_to_samples( obj, seconds );
total_chunks = SetChunkLength( obj, chunk_length_samples );
end
%%
function [chunk, num_read] = ReadChunk( obj )
% ReadChunk Reads next chunk of blocklength.
%
%# [chunk, num_read] = ReadChunk( obj )
%# OUTPUT chunk: chunk samples
%# OUTPUT num_read: number of read samples.
%
% If next chunk incomplete: returns zero.
% If next chunk not available: returns zero.
start_pos = 1 + obj.chunks_read * obj.chunk_length;
stop_pos = start_pos + obj.chunk_length -1;
if( start_pos>obj.range_length || stop_pos>obj.range_length)
num_read = 0;
chunk = 0;
% warning('Reached the end of samples, nothing read.')
% To do: handle last incomplete frame.
return;
end
chunk = obj.range_samples(start_pos:stop_pos);
num_read = 1 + stop_pos - start_pos;
obj.chunks_read = 1 + obj.chunks_read;
end
%%
function progress = Progress(obj)
% Progress Returns progress in percentages.
progress = 100.0 * double(obj.chunks_read) / double(obj.total_chunks);
end
%%
function range = GetFullRange(obj)
% GetFullRange Returns all samples available.
range = obj.range_samples;
end
%%
function fs = GetSamplingRate(obj)
% GetSamplingRate Returns sampling rate.
fs = obj.fs;
end
%%
function samples = GetNumberOfSamples(obj)
samples = obj.total_samples;
end
%%
function num_chunks_read = GetNumberOfReadChunks(obj)
% GetNumberOfReadChunks Returns number of chunks read so far.
num_chunks_read = obj.chunks_read;
end
%%
function [samples, count] = GetSamples(obj, start_sec, end_sec)
% GetSamples Call to immediately get range of samples.
[start_sec, end_sec] = obj.checks( start_sec,end_sec );
start_sample = obj.seconds_to_samples(start_sec);
stop_sample = obj.seconds_to_samples(end_sec);
count = 1 + stop_sample - start_sample;
try
samples = audioread(obj.path, [start_sample, stop_sample]);
catch
error('Problem with reading wav file. Exiting.')
end
end
%%
function [samples, count] = GetAllSamples(obj)
% GetAllSamples Call to immediately get all of the samples.
try
samples = audioread(obj.path);
catch
error('Problem with reading wav file. Exiting.')
end
count = obj.total_samples;
end
end % methods public
methods (Access=private)
% Secs to samples
function [sample] = seconds_to_samples( obj, seconds )
sample = obj.fs * seconds;
end
% Check range
function [start_checked, end_checked] = checks(obj, start_sec, end_sec)
start_checked = start_sec;
end_checked = end_sec;
% Check fatal errors
if( start_sec>obj.total_seconds ||...
start_sec>=end_sec ||...
start_sec<0 ||...
end_sec<0 )
throw(MException( 'WavReader:FatalError', 'Range error.') );
end
% Handle overflow
if( end_sec > obj.total_seconds )
end_checked = obj.total_seconds;
end
end
end % methods private
end % class

MATLAB displaying a table [duplicate]

I want to use MATLAB's printmat do display a matrix with labels. But this doesn't work for complex numbers:
N = 5;
x = rand(N,1);
y = rand(N,1);
z = x + 1i*y;
printmat([x,y,z],'fftdemo','N=1 2 3 4 5','x y x+iy');
Output:
fftdemo =
x y x+iy
N=1 0.84072 0.34998 0.84072
2 0.25428 0.19660 0.25428
3 0.81428 0.25108 0.81428
4 0.24352 0.61604 0.24352
5 0.92926 0.47329 0.92926
As you can see the imaginary part of z isn't printed.
Is there a way to get Matlab to display complex numbers or another way to achieve this?
Any print function in matlab will only print real value of imaginary number. TO get both the parts you must call them explicitly.So correct usage would be
printmat([x,y,real(z),imag(z)],'fftdemo','N=1 2 3 4 5','x y x iy');
But this wont be of any use now since both are parts are getting printed twice.
Here is a slightly altered version of printmat that will print complex numbers. Feel free to fiddle around a bit more with it for better looks :)
function [] = printmat2(a,name,rlab,clab)
%PRINTMAT Print matrix with labels.
% PRINTMAT(A,NAME,RLAB,CLAB) prints the matrix A with the row labels
% RLAB and column labels CLAB. NAME is a string used to name the
% matrix. RLAB and CLAB are string variables that contain the row
% and column labels delimited by spaces. For example, the string
%
% RLAB = 'alpha beta gamma';
%
% defines 'alpha' as the label for the first row, 'beta' for the
% second row and 'gamma' for the third row. RLAB and CLAB must
% contain the same number of space delimited labels as there are
% rows and columns respectively.
%
% PRINTMAT(A,NAME) prints the matrix A with numerical row and column
% labels. PRINTMAT(A) prints the matrix A without a name.
%
% See also: PRINTSYS.
% Clay M. Thompson 9-24-90
% Copyright 1986-2002 The MathWorks, Inc.
% $Revision: 1.10 $ $Date: 2002/04/10 06:32:35 $
error(nargchk(1,4,nargin));
space = ' ';
[nrows,ncols] = size(a);
if nargin<2, name = []; end
if nargin==3, error('Wrong number of input arguments.'); end
if nargin<4,
rlab = []; clab = [];
for i=1:nrows, rlab = [rlab, sprintf('--%d--> ',i)]; end
for i=1:ncols, clab = [clab, sprintf('--%d--> ',i)]; end
rlab=rlab(1:length(rlab)-1);
clab=clab(1:length(clab)-1);
end
col_per_scrn=5;
len=12;
if (nrows==0)|(ncols==0),
if ~isempty(name), disp(' '), disp(sprintf('%s = ',name)), end
disp(' ')
disp(' []')
disp(' ')
return
end
% Remove extra spaces (delimiters)
ndx1 = find(clab==' ');
ndx2 = find([ndx1,0]==[-1,ndx1+1]);
if ~isempty(clab), clab(ndx1(ndx2))=[]; end
ndx1 = find(rlab==' ');
ndx2 = find([ndx1,0]==[-1,ndx1+1]);
if ~isempty(rlab), rlab(ndx1(ndx2))=[]; end
% Determine position of delimiters
cpos = find(clab==' ');
if length(cpos)<ncols-1, error('Not enough column labels.'); end
cpos = [0,cpos,length(clab)+1];
rpos = find(rlab==' ');
if length(rpos)<nrows-1, error('Not enough row labels.'); end
rpos = [0,rpos,length(rlab)+1];
col=1;
n = min(col_per_scrn-1,ncols-1);
disp(' ')
if ~isempty(name), disp(sprintf('%s = ',name)), end % Print name
while col<=ncols
% Print labels
s = space(ones(1,len+1));
for j=0:n,
lab = clab(cpos(col+j)+1:cpos(col+j+1)-1);
if length(lab)>len,
lab=lab(1:len);
else
lab=[space(ones(1,len-length(lab))),lab]; end
s= [s,' ',lab];
end
disp(setstr(s))
for i=1:nrows,
s = rlab(rpos(i)+1:rpos(i+1)-1);
if length(s)>len, s=s(1:len); else s=[space(ones(1,len-length(s))),s]; end
s = [' ',s];
for j=0:n,
element = a(i,col+j);
if imag(element) ~= 0
s=[s,sprintf('%12.5f + %12.5fi',[real(element) imag(element)])];
else
if element==0,
s=[s,' 0'];
elseif (element>=1.e6)|(element<=-1.e5)|(abs(element)<.0001)
s=[s,sprintf(' %12.5e',element)];
else
s=[s,sprintf(' %12.5f',element)];
end
end
end
disp(s)
end % for
col = col+col_per_scrn;
disp(' ')
if (ncols-col<n), n=ncols-col; end
end % while
% end printmat

Break a given MATLAB string into array of sections

Suppose I have the following string:
%%
a = 2
%%
a =3
%%
t = 0:.1:pi*4;
y = sin(t);
for k = 3:2:9
%%
y = y + sin(k*t)/k;
if ~mod(k,3)
%%
display(sprintf('When k = %.1f',k));
plot(t,y)
end
end
If you notice the third section has nested sections.
I want the output to be
[ 'a = 2', 'a=3' , 't = 0:.1:pi*4; y = sin(t); for k = 3:2:9 %% y = y + sin(k*t)/k; if ~mod(k,3) %% display(sprintf('When k = %.1f',k)); plot(t,y) end end']
How can achieve this using string manipulation?
The problem is using normal regex with '%%' will break the 3rd section into 3 sections.
Assuming the logic behind this is that you want to keep any loops or conditional statements (for, while, and if) together, then you can check for '%%' but also track the number of loops/conditionals you enter and exit.
If you are going through this line by line, then the code would look like this (where the variable tline is the line of text):
% initialize counters
str_count = 0;
in_loop = 0;
% iterate through text lines
while (tline ~= -1)
% check for the %% marker but only increment string counter if not in a loop
if ~isempty(strfind(tline,'%%')) && (in_loop == 0);
str_count = str_count + 1;
else
% if no %% or in a loop, add the current line to the current string
str_set{str_count} = [str_set{str_count} tline];
% check for "for", "if", or "while" and increment the loop counter if found
if ~isempty(strfind(tline,'for ')) || ~isempty(strfind(tline,'if ')) || ~isempty(strfind(tline,'while '))
in_loop = in_loop + 1;
end
% check for "end" and decrement the loop counter if found
if ~isempty(strfind(tline,'end'));
in_loop = in_loop - 1;
end
end
end
Keep in mind that this uses strfind to look for the for, while, if, and end commands (with spaces after the first three). However, it would also pick up words that had those strings at the end (for example, the line xfor = 1; would be picked up). So you either have to add some extra checks or be careful about how you name the variables -- perhaps a better way of looking for those strings is the best. You should see spaces before the words unless they are the first words, and could add conditions to that effect.

Converting Matlab to Octave is there a containers.Map equivalent?

I am trying to convert some matlab code from the Maia package into something that will work with Octave. I am currently getting stuck because one of the files has several calls to containers.Map which is apparently something that has not yet been implemented in octave. Does anyone have any ideas for easily achieving similar functionality without doing a whole lot of extra work in octave? Thanks all for your time.
function [adj_direct contig_direct overlap names longest_path_direct...
weigth_direct deltafiles deltafiles_ref ReferenceAlignment ...
contig_ref overlap_ref name_hash_ref] = ...
assembly_driver(assemblies,ref_genome,target_chromosome, ...
deltafiles_ref,contig_ref, overlap_ref, ...
name_hash_ref, varargin)
% ASSEMBLY_DRIVER Combines contig sets into one assembled chromosome
%
% INPUT
% assemblies
% ref_chromosome
% Startnode_name
% Endnode_name
% OPTIONAL DEFAULT
% 'z_weigths' [.25 .25 .25 .25]
% 'clipping_thrs' 10
% 'ref_distance' -10
% 'ref_quality' 1E-5
% 'max_chromosome_dist' 100
% 'quit_treshold' 15
% 'tabu_time' 3
% 'minimum_improvement' -inf
% 'ref_node_assemblies' all assemblies (slow)
% 'endextend' true
%
%
% SET DEFAULTS
% General parameters
z_weights = [.25 .25 .25 .25];
clipping_thrs = 10;
mapfilter = '-rq';
alignlen = 75;
ident = 85;
% Reference nod parameters
ref_distance = -10;
ref_quality = 1E-5;
max_chromosome_dist = 100;
% TABU parameters
quit_treshold = 15;
tabu_time = 3;
minimum_improvement = -inf;
ref_node_assemblies = assemblies;
% Extending the assembly outwards from the start and en node
endextend = true;
AllowReverse = true;
% If no start and end node are given, they will be determined from tiling
Startnode_name = '';
Endnode_name = '';
containment_edge = true;
ref_first = true;
% If contigs have already been aligned to the reference, give the
% deltafile
ReferenceAlignment = 'NotYetDoneByMaia';
% Get VARARGIN user input
if length(varargin) > 0
while 1
switch varargin{1}
case 'Startnode_name'
Startnode_name = varargin{2};
case 'Endnode_name'
Endnode_name = varargin{2};
case 'z_weigths'
z_weights = varargin{2};
case 'clipping_thrs'
clipping_thrs = varargin{2};
case 'ref_distance'
ref_distance = varargin{2};
case 'ref_quality'
ref_quality = varargin{2};
case 'max_chromosome_dist'
max_chromosome_dist = varargin{2};
case 'quit_treshold'
quit_treshold = varargin{2};
case 'tabu_time'
tabu_time = varargin{2};
case 'minimum_improvement'
minimum_improvement = varargin{2};
case 'ref_node_assemblies'
ref_node_assemblies = assemblies(varargin{2},:);
case 'extend_ends'
endextend = assemblies(varargin{2},:);
case 'AllowReverse'
AllowReverse = varargin{2};
case 'ReferenceAlignment'
ReferenceAlignment = varargin{2};
case 'containment_edge'
containment_edge = varargin{2};
case 'ref_first'
ref_first = varargin{2};
case 'mapfilter'
mapfilter = varargin{2};
case 'alignlen'
alignlen = varargin{2};
case 'ident'
ident = varargin{2};
otherwise
error(['Input ' varargin{2} ' is not known']);
end
if length(varargin) > 2
varargin = varargin(3:end);
else
break;
end
end
end
% Read input assemblies
assembly_names = assemblies(:,1);
assembly_locs = assemblies(:,2);
assembly_quality = containers.Map(assemblies(:,1),assemblies(:,3));
assembly_quality('reference') = ref_quality;
% Read input assemblies for creation of reference nodes
ref_node_assembly_names = ref_node_assemblies(:,1);
ref_node_assembly_locs = ref_node_assemblies(:,2);
ref_node_assembly_quality = containers.Map(ref_node_assemblies(:,1),ref_node_assemblies(:,3));
ref_node_assembly_quality('reference') = ref_quality;
% If there is only one assembly there is nothing to align
if size(assemblies,1) >= 2
% Align assemblies against each other
assembly_pairs = {};
coordsfiles = [];
deltafiles = [];
for i = 1:length(assembly_locs)-1
for j = i+1:length(assembly_locs)
[coordsfile,deltafile] = align_assemblies({assembly_locs{i},assembly_locs{j}},{assembly_names{i}, assembly_names{j}}, ...
mapfilter, alignlen, ident);
coordsfiles = [coordsfiles; coordsfile];
%deltafiles = [deltafiles deltafile];
deltafiles = [deltafiles; {deltafile}];
assembly_pairs = [assembly_pairs;[assembly_names(i) assembly_names(j)]];
end
end
% fprintf('Loading alignment files.\n');
% load alignments_done;
% Put the nucmer alignments in an adjency matrix
%[adj, names, name_hash, contig, overlap] = get_adj_matrix(coordsfiles, assembly_pairs, assembly_quality, z_weights, 'clipping_thrs', clipping_thrs, 'dove_tail', 'double','edge_weight','z-scores', 'containment_edge', true);
[adj, names, name_hash, contig, overlap] = get_adj_matrix(deltafiles, assembly_pairs, assembly_quality, z_weights, 'clipping_thrs', clipping_thrs, 'dove_tail', 'double','edge_weight','z-scores', 'containment_edge', containment_edge);
% Merge deltafiles
deltafilesnew = deltafiles{1};
if size(deltafiles,1) > 1
for di = 2:size(deltafiles,1)
deltafilesnew = [deltafilesnew deltafiles{di}];
end
end
deltafiles = deltafilesnew;
else
assembly_pairs = {};
coordsfiles = [];
deltafiles = [];
adj = [];
names = {};
name_hash = containers.Map;
contig = struct('name',{},'size',[],'chromosome',[],'number',[], 'assembly', [], 'assembly_quality', []);
overlap = struct('Q',{},'R',[],'S1',[],'E1', [], 'S2', [], 'E2', [], 'LEN1', [], 'LEN2', [], 'IDY', [], 'COVR', [], 'COVQ', [],'LENR',[], 'LENQ',[]);
end
% Ad the pseudo nodes to the graph. If the contigs have already been
% aligned to the reference genome, just select the alignments that
% correspond to the target chromosome
if isequal(ReferenceAlignment,'NotYetDoneByMaia')
% Align all contigs in 'contig_sets_fasta' to the reference chromosome
[contig_ref, overlap_ref, name_hash_ref, deltafiles_ref] = align_contigs_sets(...
ref_genome, ref_node_assembly_locs, ref_node_assembly_names, ...
ref_node_assembly_quality, clipping_thrs, z_weights, ...
ref_distance,max_chromosome_dist);
ReferenceAlignment = 'out2.delta';
end
% Select only the entries in the deltafile for the current target chromosome
[contig_target_ref, overlap_target_ref, name_hash_target_ref, delta_target_ref] = ...
GetVariablesForTargetChromosome(...
contig_ref, overlap_ref, deltafiles_ref);
% Ref clipping should be high in case of tiling
%if isequal(max_chromosome_dist,'tiling')
% clipping_thrs = 10000
%end
% Add reference nodes to the adjency matrix
[adj, names, name_hash, contig, overlap, delta_target_ref, Startnode_name, Endnode_name] = get_reference_nodes( ...
adj, names, name_hash, contig, overlap, target_chromosome, ...
contig_target_ref, overlap_target_ref, name_hash_target_ref, delta_target_ref, ...
max_chromosome_dist, ref_distance, clipping_thrs, ref_first,...
Startnode_name, Endnode_name, AllowReverse);
% Give reference edges some small extra value to distict between
% assemblies to which a reference node leads
% adj = rank_reference_edges(adj,contig,assembly_quality);
% Specify a start and an end node for the assembly
Startnode = name_hash(Startnode_name);
Endnode = name_hash(Endnode_name);
% Find the best scoring path
fprintf('Directing the final graph\n');
% Calculate path on undirected graph to get an idea on how to direct the graph
[longest_path weigth] = longest_path_tabu(adj, Startnode, Endnode, quit_treshold, tabu_time, minimum_improvement);
% Make the graph directed (greedy)
[adj_direct contig_direct] = direct_graph(adj,overlap, contig, names, name_hash,clipping_thrs, Startnode, longest_path, true, ref_first);
% Calcultate final layout-path
fprintf('Find highest scoring path\n');
[longest_path_direct weigth_direct] = longest_path_tabu(adj_direct, Startnode, Endnode, quit_treshold, tabu_time, minimum_improvement);
function [contig_target_ref, overlap_target_ref, name_hash_target_ref, delta_target_ref] = ...
GetVariablesForTargetChromosome(...
contig_ref, overlap_ref, deltafiles_ref)
% Select only the entries in the deltafile for the current target chromosome
delta_target_ref = deltafiles_ref;
for di = size(delta_target_ref,2):-1:1
if ~isequal(delta_target_ref(di).R,target_chromosome)
delta_target_ref(di) = [];
end
end
overlap_target_ref = overlap_ref;
for oi = size(overlap_target_ref,2):-1:1
if ~isequal(overlap_target_ref(oi).R,target_chromosome)
overlap_target_ref(oi) = [];
end
end
contig_target_ref = contig_ref;
for ci = size(contig_target_ref,1):-1:1
if isequal(contig_target_ref(ci).assembly, 'reference') && ~isequal(contig_target_ref(ci).name,target_chromosome)
contig_target_ref(ci) = [];
end
end
name_hash_target_ref = make_hash({contig_target_ref.name}');
end
end
There is no exact equivalent of containers.Map in Octave that I know of...
One option is to use the java package to create java.util.Hashtable. Using this example:
pkg load java
d = javaObject("java.util.Hashtable");
d.put('a',1)
d.put('b',2)
d.put('c',3)
d.get('b')
If you are willing to do a bit of rewriting, you can use the builtin struct as a rudimentary hash table with strings (valid variable names) as keys, and pretty much anything stored in values.
For example, given the following:
keys = {'Mon','Tue','Wed'}
values = {10, 20, 30}
you could replace this:
map = containers.Map(keys,values);
map('Mon')
by:
s = struct();
for i=1:numel(keys)
s.(keys{i}) = values{i};
end
s.('Mon')
You might need to use genvarname to produce valid keys, or maybe a proper hashing function that produces valid key strings.
Also look into struct-related functions: getfield, setfield, isfield, fieldnames, rmfield, etc..