필터 지우기
필터 지우기

Text file for loop help

조회 수: 1 (최근 30일)
jgillis16
jgillis16 2016년 5월 12일
답변: dpb 2016년 5월 13일
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)

답변 (1개)

dpb
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...

카테고리

Help CenterFile Exchange에서 Text Files에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by