YLM
YLM

Reputation: 345

Save data in text files with a concatened name

I am loading my files into matlab. I import a .txt file using importdata, then I process the files and then I would like to save some results in different text files. For example if I process: 'toto.txt'

At the end I would like to save 2 texts files name : 'toto_part1.txt' 'toto_part2.txt' and so on if I have more than 2 parts.

Any ideas? Thank you

Upvotes: 1

Views: 51

Answers (1)

Alexander Korovin
Alexander Korovin

Reputation: 1475

While you did not provide an example I will try to explain my comment under your question. Let we have a text file with text and numerical data:

toto.txt

Col1 Col2 Col3
1    2    3
4    5    6

Next we use the importdata function to load file and separate numerical and text data:

tmp = importdata('toto.txt'); % load file to a struct
matrix = tmp.data; % save data to a matrix

And now we save some data (for example, columns):

% save all columns in separated files
for n=1:size(matrix,2)
      out = matrix(:,n); % extract n-th column 
      % save in ascii format with tabs separator
      save(['toto',num2str(n),'.txt'],'out','-ascii', '-tabs');
end

Here you can see an example of the string concatenation: ['toto',num2str(n),'.txt']

Upvotes: 2

Related Questions