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 Center および File ExchangeText Files についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by