saving files after export with the same name as original file

hello!
I have several .asc files with different names. After filter this data I want to export each file to a .xlsx file and save each file with the same name as original .asc file. I'm not able to achive this last thing.
a sample of the different .asc file names:
6969_2020_11_23_0001_INT-17_filter
6969_2020_11_23_0002_T200-5_filter
I hope someone can help me with this issue. I attach the code.
Thanks a lot.
Kind regards,
close all
clear
clc
archivos=ls('*.asc'); %crea una lista con los archivos .asc
for i=1:length(archivos(:,1)) %bucle que recorra todos los archivos
%importo y selecciono los datos
estructura_datos=importdata(archivos(i,:));
datos=estructura_datos.data;
%ordeno por profundidad ascendente
ordenado_por_profundidad=sortrows(datos,1);
%localizar y eliminar datos erróneos
posicion_datos_erroneos=find(ordenado_por_profundidad(:,19)==-9.990e-29); %localiza flags erróneos
ordenado_por_profundidad(posicion_datos_erroneos,:)=[]; %elimina filas donde hay flag erróneo
%localizar y eliminar profundidad < 1m
posicion_menor_1m=find(ordenado_por_profundidad(:,1)<1); %localiza posiciones <1m
ordenado_por_profundidad(posicion_menor_1m,:)=[]; % elimina filas con profundidades <1 m
%sustituyo columnas de salinidad por las corregidas
ordenado_por_profundidad(:,[3 4 17 18])=ordenado_por_profundidad(:,[17 18 3 4]); %corrijo la salinidad
ordenado_por_profundidad(:,[13:15 17 18 19])=[]; %elimino columnas que no me interesan
%creo una tabla
Profundidad = ordenado_por_profundidad(:,1);
Temperatura = ordenado_por_profundidad(:,2);
Salinidad = ordenado_por_profundidad(:,3);
Sigma = ordenado_por_profundidad(:,4);
Clorofila = ordenado_por_profundidad(:,5);
Turbidez = ordenado_por_profundidad(:,6);
Ox = ordenado_por_profundidad(:,7);
Ox2 = ordenado_por_profundidad(:,8);
Cloi = ordenado_por_profundidad(:,9);
Par = ordenado_por_profundidad(:,10);
Ph = ordenado_por_profundidad(:,11);
Carb = ordenado_por_profundidad(:,12);
Tiempo = ordenado_por_profundidad(:,13);
Tabla=table(Profundidad,Temperatura,Salinidad,Sigma,Clorofila,Turbidez,Ox,Ox2,Cloi,Par,Ph,Carb,Tiempo);
%exporto los datos a formato excel
writetable(Tabla,['6969_2020_11_23_' num2str(i) '.xlsx'],'Sheet',1,'Range','A1');
end

3 Comments

I'm actually not sure how ls is working here. If you use dir you will have the name of the file in a struct, then you can use strrep(files(i).name,'asc','xlsx')
ls saves the folder oontents to a cell array. It does still work, but at least on my machine, you'd want to start the for loop at 3 since the first 2 entries were . and ..
Still, dir is the better choice.

Sign in to comment.

 Accepted Answer

Try this:
archivos = dir('*.asc'); %crea una lista con los archivos .asc
for k=1:length(archivos) %bucle que recorra todos los archivos
inputFileName = fullfile(archivos(k).folder, archivos(k).name);
% Output filename is the same except for a .xlsx extension instead of .asc.
outputFileName = strrep(lower(inputFileName), '.asc', '.xlsx');
% remainder of your code follows.
% etc.
%exporto los datos a formato excel
writetable(Tabla, outputFileName, 'Sheet',1, 'Range','A1');
end

4 Comments

Just read Matt's prior comment and noticed it's pretty much the same. Matt, put answers down here in the official Answers section so you can get reputation points for them.
Why? This works. Unless you care about the capitalization, but I didn't think that was important.
If maintaining the same upper or lower case, character by character, is important, then you can use fileparts() like this:
archivos = dir('*.m'); %crea una lista con los archivos .asc
for k=1:length(archivos) %bucle que recorra todos los archivos
inputFileName = fullfile(archivos(k).folder, archivos(k).name);
% Output filename is the same except for a .xlsx extension instead of .asc.
[f, baseFileNameNoExt, ext] = fileparts(inputFileName);
outputFileName = fullfile(f, [baseFileNameNoExt, '.xlsx']);
% Remainder of your code follows.
% etc.
% Exporto los datos a formato excel
fprintf('%s\n', outputFileName);
writetable(Tabla, outputFileName, 'Sheet',1, 'Range','A1');
end
fprintf('Done running %s.m ...\n', mfilename);
Alberto, I did this with your actual data files and it worked perfectly. I think you didn't incorporate my code correctly, especially when sending the input file name into importdata():
clear all
clc
archivos = dir('*.asc'); %list with .asc files
for k=1:length(archivos) %loop
fprintf('\nProcessing file #%d of %d...\n', k, length(archivos));
inputFileName = fullfile(archivos(k).folder, archivos(k).name);
% Output filename is the same except for a .xlsx extension instead of .asc.
outputFileName = strrep(lower(inputFileName), '.asc', '.xlsx');
%import and select data
estructura_datos=importdata(inputFileName);
datos=estructura_datos.data;
%sort by increasing depth
ordenado_por_profundidad=sortrows(datos,1);
%locate and delete outliers
posicion_datos_erroneos=find(ordenado_por_profundidad(:,19)==-9.990e-29); %localiza flags erróneos
ordenado_por_profundidad(posicion_datos_erroneos,:)=[]; %elimina filas donde hay flag erróneo
%locate and delete depth < 1m
posicion_menor_1m=find(ordenado_por_profundidad(:,1)<1); %localiza posiciones <1m
ordenado_por_profundidad(posicion_menor_1m,:)=[]; % elimina filas con profundidades <1 m
%change salinity columns and delete the non-corrected ones
ordenado_por_profundidad(:,[3 4 17 18])=ordenado_por_profundidad(:,[17 18 3 4]); %corrijo la salinidad
ordenado_por_profundidad(:,[13:15 17 18 19])=[]; %elimino columnas que no me interesan
%create a table
Profundidad = ordenado_por_profundidad(:,1);
Temperatura = ordenado_por_profundidad(:,2);
Salinidad = ordenado_por_profundidad(:,3);
Sigma = ordenado_por_profundidad(:,4);
Clorofila = ordenado_por_profundidad(:,5);
Turbidez = ordenado_por_profundidad(:,6);
Ox = ordenado_por_profundidad(:,7);
Ox2 = ordenado_por_profundidad(:,8);
Cloi = ordenado_por_profundidad(:,9);
Par = ordenado_por_profundidad(:,10);
Ph = ordenado_por_profundidad(:,11);
Carb = ordenado_por_profundidad(:,12);
Tiempo = ordenado_por_profundidad(:,13);
Tabla=table(Profundidad,Temperatura,Salinidad,Sigma,Clorofila,Turbidez,Ox,Ox2,Cloi,Par,Ph,Carb,Tiempo);
% etc.
%exporto los datos a formato excel
fprintf('Input File : %s\nOutput File : %s\n', inputFileName, outputFileName);
writetable(Tabla, outputFileName, 'Sheet',1, 'Range','A1');
end
Thank you so much!
You're right and I don't hesitate about it.
I'm sorry but I'm trying to resume Matlab after a few years and I'm a little bit rusty. I'll keep learning because I want to improve.
Thanks a lot!
Take care!

Sign in to comment.

More Answers (1)

Dear all,
thank you very much for your answers! I'm sorry but I forgot to attach sample files. I do it now.
My script works well, but the only thing that I want to improve is exporting the .asc file to an .xls file with exactly the same name as the original .asc file.
I tried with the code from Image Analyst (thank you very much ;)) but I'm afraid I make some mistake.
Now you can find my original code with 2 sample files.
Thank you very much to all of you!
Kind regards
close all
clear
clc
archivos=ls('*.asc'); %list with the .asc files
for i=1:length(archivos(:,1)) %bloop
%import and select data
estructura_datos=importdata(archivos(i,:));
datos=estructura_datos.data;
%sort by increasing depth
ordenado_por_profundidad=sortrows(datos,1);
%locate and erase outliers
posicion_datos_erroneos=find(ordenado_por_profundidad(:,19)==-9.990e-29); %localiza flags erróneos
ordenado_por_profundidad(posicion_datos_erroneos,:)=[]; %elimina filas donde hay flag erróneo
%locate and erase depth <1 m
posicion_menor_1m=find(ordenado_por_profundidad(:,1)<1); %localiza posiciones <1m
ordenado_por_profundidad(posicion_menor_1m,:)=[]; % elimina filas con profundidades <1 m
%change columns and erase the columns that I don't need
ordenado_por_profundidad(:,[3 4 17 18])=ordenado_por_profundidad(:,[17 18 3 4]); %corrijo la salinidad
ordenado_por_profundidad(:,[13:15 17 18 19])=[]; %elimino columnas que no me interesan
%create a table
Profundidad = ordenado_por_profundidad(:,1);
Temperatura = ordenado_por_profundidad(:,2);
Salinidad = ordenado_por_profundidad(:,3);
Sigma = ordenado_por_profundidad(:,4);
Clorofila = ordenado_por_profundidad(:,5);
Turbidez = ordenado_por_profundidad(:,6);
Ox = ordenado_por_profundidad(:,7);
Ox2 = ordenado_por_profundidad(:,8);
Cloi = ordenado_por_profundidad(:,9);
Par = ordenado_por_profundidad(:,10);
Ph = ordenado_por_profundidad(:,11);
Carb = ordenado_por_profundidad(:,12);
Tiempo = ordenado_por_profundidad(:,13);
Tabla=table(Profundidad,Temperatura,Salinidad,Sigma,Clorofila,Turbidez,Ox,Ox2,Cloi,Par,Ph,Carb,Tiempo);
%export to excel (I WANT THE SAME NAME AS .asc ORIGINAL)
writetable(Tabla,['6969_2020_11_23_' num2str(i) '.xlsx'],'Sheet',1,'Range','A1');
end

1 Comment

My code correctly constructs the filename. I tested it. What did you do wrong? You forgot to attach your code after you incorporated my code into it. Please do so, so I can fix it.

Sign in to comment.

Products

Release

R2019a

Community Treasure Hunt

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

Start Hunting!