Skip to content

Instantly share code, notes, and snippets.

@williamrowell
Last active August 29, 2015 14:24
Show Gist options
  • Save williamrowell/b051a47aaa7298b7df5c to your computer and use it in GitHub Desktop.
Save williamrowell/b051a47aaa7298b7df5c to your computer and use it in GitHub Desktop.
MATLAB helper functions
function cellArray = cellFromTextFile(fileName)
% Loads cell array of strings, 1xN character arrays, from the given text
% file.
if exist(fileName, 'file') == 0
error('unable to read file, %s, does not exist', fileName);
end
try
cellArray = textread(fileName, '%s');
catch ME
error('unable to read file, %s: %s', ME.message);
end
% Reshape cell array so that it is 1xN
[nrow,ncol] = size(cellArray);
cellArray = reshape(cellArray,[ncol,nrow]);
end
function cellToTextFile(fileName,cellArray)
% Saves a cell array of strings to a simple text file. The cell array must
% have size = [1,N] and consisit entierly of character arrays (strings).
% Check that cell array has correct format for saving
% try
% checkCellArray(cellArray);
% catch ME
% error('unable to write cell array: %s', ME.message);
% end
% Try and open file.
[fid, msg] = fopen(fileName,'w');
if fid == -1
error('unable to open file, %s, for writing: %s', fileName, msg);
end
% Write contents of cell array to file and close file when done.
for i = 1:length(cellArray)
fprintf(fid,'%s\n', cellArray(i).name);
end
fclose(fid);
end
function checkCellArray(cellArray)
% Check that cell array has the correct format for saving data. It must be
% have size [1,N] and all element must be [1,M] character arrays.
[nrow,ncol] = size(cellArray);
if nrow ~= 1
error('cell array must have size = [1,N] in order to save');
end
for i = 1:ncol
string = cellArray{i};
if ~ischar(string)
error('cell array elements must be character arrays');
end
[n,m] = size(string);
if n~=1
error('cell array elements must be [1,N] character arrays');
end
end
end
function [ string ] = flattenCellArray( cell_array )
%FLATTENCELLARRAY
% Accepts cell array of strings as input and returns a comma separated
% string with all elements of the cell array.
if iscell(cell_array)
string = cell_array{1};
for idx=2:length(cell_array)
string = [string,', ',cell_array{idx}];
end
else
error('%s is not a cell array of strings',cell_array);
end
end
function [ output ] = left_join( left_file, right_file, out_file )
%left_join LEFT JOIN excel spreadsheet 1 to excel spreadsheet 2
% Return a table and excel spreadsheet that is a LEFT JOIN of excel
% spreadsheet 1 and excel spreadsheet 2 on the first column in each file.
% The output will have a row for every row in spreadsheet 1, and if there
% is a corresponding data in spreadsheet 2, it will be appended to the
% data from spreadsheet 1. The output will be the joined spreadsheet.
% If a third argument is provided, the output will be written to an excel
% file with the name provided.
% REQUIREMENTS:
% 1) The first row in each file is the header row.
% 2) The first column in each file is to be used for the join (i.e.
% line_name).
% 3) The values in the first column of spreadsheet 2 are unique (i.e.
% each value only appears once).
% open spreadsheets
[~, ~, left_side] = xlsread(left_file);
[~, ~, right_side] = xlsread(right_file);
% check that spreadsheet 2 column 1 values are unique
if length(right_side(2:length(right_side),1)) > length(unique(right_side(2:length(right_side),1)))
error('The first column of %s has non-unique values.', right_file)
end
% grow spreadsheet 1 and copy headers from spreadsheet 2 to spreadsheet 1
left_cells = length(left_side(1,:));
right_cells = length(right_side(1,:));
left_side = horzcat(left_side, cell(length(left_side(:,1)),right_cells)); % grow left_side
left_side(1,left_cells+1:left_cells+right_cells) = right_side(1,:); % copy headers
% for each row in spreadsheet 1, look for corresponding data in spreadsheet
% 2 and append data
for left_idx = 2:length(left_side)
for right_idx = 2:length(right_side)
if strcmp(left_side{left_idx,1},right_side{right_idx,1})
left_side(left_idx,left_cells+1:left_cells+right_cells) = right_side(right_idx,:);
end
end
end
% write the output to a file
if nargin == 3
xlswrite(out_file, left_side);
end
% export the output
output = left_side;
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment