reconstruct text file after importdata?

4 views (last 30 days)
James
James on 22 Jun 2013
Hi,
I am using matlab to rewrite a .txt file after I change parts of the file. My function reads in a .txt file using the importdata function. The text file is a csound file, rather a file that can be read and played by the sound synthesis engine csound. However, there are problems with these files, and I am using matlab to fix those problems in a programmatic way. The importdata function seems to work perfectly because it splits my file up into textdata that I don't need to change, and data that I do. So I use importdata, then I do computations on the numerical data to change it to what I want, and now I need to put it back together. However, there doesn't seem to be a complimentary function to the importdata function. Or is there? Heres some code:
function tienotes(infile, outfilename)
% FUNCTION VARIABLES
inID = fopen(infile, 'r');
outID = fopen(outfilename, 'wb');
% GET HEARDERLINES OF INPUT FILE
HEADERLINES = 0;
line = fgetl(inID); % returns current line of file
while ischar(line)
if numel(line) == 0
HEADERLINES = HEADERLINES + 1;
elseif line(1) == 'i'
break
else
HEADERLINES = HEADERLINES + 1;
end
line = fgetl(inID);
end
% IMPORT THE FILE
newData = importdata(infile, ' ', HEADERLINES);
% Create new variables in the base workspace from those fields.
vars = fieldnames(newData);
for i = 1:length(vars)
assignin('base', vars{i}, newData.(vars{i}));
end
% TIE NOTES
data = newData.data;
[numrows, numcols] = size(data);
for m = 1:numrows
for n = m:numrows
if(data(m,1) + abs(data(m,2)) == data(n,1)) % check if start time
% plus duration in one row is equal to start time of another
% row, abs for if note is already tied
if(data(m,3) == data(n,3)) % check if note indeces are the same
if(data(m,5) == data(n,5)) % check if chan #s are the same
if(sign(data(m,2)) == 1) % if positive value
data(m,2) = -(data(m,2) + data(n,2)); % neg for tie
elseif(sign(data(m,2)) == -1) % if negative value, then
% its already tieing some notes, but you cant add
% to the negative, so you need to subtract!
data(m,2) = data(m,2) - data(n,2);
end
end
end
end
end
end
% PUT NEW DATA INTO OUTPUT FILE
% ???
% CLOSE STUFF
fclose(inID);
fclose(outID);
end

Answers (0)

Tags

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!