Text file for loop help
조회 수: 1 (최근 30일)
이전 댓글 표시
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
답변 (1개)
dpb
2016년 5월 13일
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
참고 항목
카테고리
Help Center 및 File Exchange에서 Text Files에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!