saving files after export with the same name as original file
2 views (last 30 days)
Show older comments
Alberto Martínez
on 17 Dec 2020
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
Cris LaPierre
on 18 Dec 2020
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.
Stephen23
on 18 Dec 2020
Edited: Stephen23
on 19 Dec 2020
"...you'd want to start the for loop at 3 since the first 2 entries were . and .."
Misleading advice. Better advice:
As Walter Roberson wrote in that last link: "In short: if your code assumes that '.' and '..' are the first two entries in a directory, your code has a bug (even in MS Windows). If your code assumes that directory entries are returned in any sorted order, your code has a bug (in all OS.)"
Accepted Answer
Image Analyst
on 18 Dec 2020
Edited: Image Analyst
on 18 Dec 2020
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
Image Analyst
on 18 Dec 2020
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
More Answers (1)
Alberto Martínez
on 18 Dec 2020
1 Comment
Image Analyst
on 18 Dec 2020
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.
See Also
Categories
Find more on Standard File Formats in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!