saving files after export with the same name as original file

2 次查看(过去 30 天)
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 个评论
Cris LaPierre
Cris LaPierre 2020-12-18
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
Stephen23 2020-12-18
编辑:Stephen23 2020-12-19

请先登录,再进行评论。

采纳的回答

Image Analyst
Image Analyst 2020-12-18
编辑:Image Analyst 2020-12-18
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 个评论
Image Analyst
Image Analyst 2020-12-18
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
Alberto Martínez
Alberto Martínez 2020-12-18
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!

请先登录,再进行评论。

更多回答(1 个)

Alberto Martínez
Alberto Martínez 2020-12-18
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 个评论
Image Analyst
Image Analyst 2020-12-18
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.

请先登录,再进行评论。

类别

Help CenterFile Exchange 中查找有关 Standard File Formats 的更多信息

产品


版本

R2019a

Community Treasure Hunt

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

Start Hunting!

Translated by