Text file for loop help
1 view (last 30 days)
Show older comments
I have a text file (attached), which has 4 columns. I wanted to create separate text files out of this one text file based on the first column that is divided up into numbers (11,12,13,...) all the way until 290. I also wanted to pull out the first row of every text file and put it into one huge text file.
Below is an example of the code written, but I do not know how to integrate a for loop into this to perform those two actions.
clear all
fidi = fopen('1month290lags.txt','rt');
Glxc = textscan(fidi, '%s', 'Delimiter','|');
frewind(fidi)
Glxcs = textscan(fidi, '%s', 'EndOfLine','\r\n');
fclose(fidi);
dlen = 4*fix(length(Glxc{:})/4); % Set Row Length
Glxcr = reshape(Glxc{:}(1:dlen), 4, [])'; % Reshape & Transpose
LIdx=str2double(Glxcr(:,1))<=11
NewGlxc = Glxcs{:}(LIdx,:); % Rows Of New Array
fido = fopen('1month290lags11.txt','wt')
fprintf(fido, '%s\n', NewGlxc{:});
fclose(fido)
0 Comments
Answers (1)
dpb
on 13 May 2016
Glxc = textscan(fidi, '%s', 'Delimiter','|');
Why are you reading the numeric data as strings? Read it as numeric and the operations on finding the desired values then become trivial.
x=cell2mat(textscan(fidi, '', 'Delimiter','|'));
u=unique(x(:,1); % find the unique values in the first column
for i=1:length(u) % over all unique values
dlmwrite('YourdesiredfilenameforUniqueCase(i)',x(x(:,1)==u(i),:)) % write data for each u
end
I don't understand the other request, sorry...
0 Comments
See Also
Categories
Find more on Text Files in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!