Discover MakerZone

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn more

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today

Thread Subject:
Exporting ascii data in a loop

Subject: Exporting ascii data in a loop

From: info_maniac

Date: 5 Jul, 2012 20:07:08

Message: 1 of 3

Hi all,

I am stuck in this simple exporting and I would greatly appreciate your help.

I am writing some code that imports many *.txt files (eg. R12HR13.txt), then 'smooths' them one by one and saves the smoothed results back to new *.txt files.
I would like the new files to have the same name as the imported ones, and if possible with the word new added (eg. R12HR13new.txt).
I cannot make the saving work, although I have tried many things.
This is my code:

txtFiles = dir('*.txt');
numfiles = length(txtFiles);
mydata = cell(1, numfiles);
smoothened = cell(1, numfiles);

for k = 1:numfiles
  mydata{k} = importdata(txtFiles(k).name);
  smoothened{k} = smooth (mydata{1,k}, 'rlowess');
  save('txtFiles(1,i).name', 'smoothened{i}', '-ASCII');
end


I also tried using fopen and fprintf within the loop to write the files, but it wouldn't work either:
for k = 1:numfiles
fid = fopen('txtFiles(k).name.txt','w');
fprintf(fid,'%d ',smoothened{1,k});
fprintf(fid,'\n');
fclose(fid);
end


Also, I would like to ask if you could tell me a way to give a path to the
txtFiles = dir('*.txt');
so that I can apply the code to many different folders, instead of changing the current folder every time.


Thank you for your time,
ziggy.

Subject: Exporting ascii data in a loop

From: Sadik

Date: 6 Jul, 2012 02:07:19

Message: 2 of 3

Hi Ziggy,

% Do this to get the list from any directory
directoryToUse = 'C:\SomeDirectory\';
txtFiles = dir([directoryToUse '*.txt']);

numFiles = length(txtFiles);

% The name between the single quotes must be a variable name. Therefore, you need to assign the values to variables rather than a cell array. If you need them for later use, then define both the cell arrays as you did, and the variables that are defined below:

for k = 1:numfiles
  data = importdata(txtFiles(k).name);
  % You don't need to write {1,k}. just {k} is enough in your case.
  smoothData = smooth (mydata{k}, 'rlowess');
  % 1) txtFiles(k).name, rather than ...(i).name
  % 2) txtFies(k).name is already a string. No need to put it between quotes.
  % 3) We add the "new" suffix now.
  save([txtFiles(i).name(1:end-4) 'new.txt'], 'smoothData', '-ASCII');
end

Best.

Subject: Exporting ascii data in a loop

From: info_maniac

Date: 6 Jul, 2012 11:42:07

Message: 3 of 3

"Sadik " <sadik.hava@gmail.com> wrote in message <jt5h8m$128$1@newscl01ah.mathworks.com>...
> Hi Ziggy,
>
> % Do this to get the list from any directory
> directoryToUse = 'C:\SomeDirectory\';
> txtFiles = dir([directoryToUse '*.txt']);
>
> numFiles = length(txtFiles);
>
> % The name between the single quotes must be a variable name. Therefore, you need to assign the values to variables rather than a cell array. If you need them for later use, then define both the cell arrays as you did, and the variables that are defined below:
>
> for k = 1:numfiles
> data = importdata(txtFiles(k).name);
> % You don't need to write {1,k}. just {k} is enough in your case.
> smoothData = smooth (mydata{k}, 'rlowess');
> % 1) txtFiles(k).name, rather than ...(i).name
> % 2) txtFies(k).name is already a string. No need to put it between quotes.
> % 3) We add the "new" suffix now.
> save([txtFiles(i).name(1:end-4) 'new.txt'], 'smoothData', '-ASCII');
> end
>
> Best.


Hi Sadik,

Thank you very much for your help, now everything makes sense.
It works now!
I am very grateful.

Best Regards,
ziggy.

Tags for this Thread

What are tags?

A tag is like a keyword or category label associated with each thread. Tags make it easier for you to find threads of interest.

Anyone can tag a thread. Tags are public and visible to everyone.

Contact us