saving files after export with the same name as original file
Show older comments
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
Matt Gaidica
on 17 Dec 2020
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')
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.
"...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
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.
Categories
Find more on Logical 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!