How to access folders within a loop

2 visualizaciones (últimos 30 días)
Daphne PARLIARI
Daphne PARLIARI el 11 de En. de 2020
Comentada: Stephen23 el 12 de En. de 2020
Hello guys!
I am struggling to do something that I don't know if it's possible but let's give it a try.
I have a set of data I want to access and since they are quite a few, I want to do it within a loop. To do so, I created a .txt (see attached) and based on its columns (Station and Network), I want to navigate through the respective directories.
To give an example, if the station is Airport and the network is EMY (as appeared in the .txt), I want the code to be able to find and read the file C:\PhD\Projects\LIFE ASTI\C.3\Weather station data\from desktop\Obs. data 4 Keppas\EMY\2015\Airport.xlsx
obsdir='C:\PhD\Projects\LIFE ASTI\C.3\Weather station data\from desktop\Obs. data 4 Keppas'
stations = readtable('C:\PhD\Projects\LIFE ASTI\C.3\Weather station data\from desktop\Stations coordinates3.txt');
for i=1:size(stations,1)
name = stations( i, 1 );
network = stations (i, 'Network');
lat = stations(i, 'Lat' );
lon = stations(i, 'Lon' );
network.(1);
name.(1);
networkstr = char(network.(1));
namestr = char(name.(1));
[obsdata,txt,raw] = xlsread([obsdir,'\',networkstr,'\', '2015','\','Airport.xlsx']);
end
Of course I am getting two errors!
Error using xlsread (line 132)
XLSREAD unable to open file 'C:\PhD\Projects\LIFE ASTI\C.3\Weather station data\from desktop\Obs. data
4 Keppas\MoT\2015\Airport.xlsx'.
File 'C:\PhD\Projects\LIFE ASTI\C.3\Weather station data\from desktop\Obs. data 4
Keppas\MoT\2015\Airport.xlsx' not found.
Error in Untitled3 (line 23)
[obsdata,txt,raw] = xlsread([obsdir,'\',networkstr,'\', '2015','\','Airport.xlsx']);
Is there any idea please??

Respuestas (1)

Meg Noah
Meg Noah el 12 de En. de 2020
Editada: Meg Noah el 12 de En. de 2020
Try this (in absence of the Airport.xlsx files I can't completely test it here) - edit the obsdir back to your folder name where the files are:
obsdir='airportData';
stations = readtable(fullfile(obsdir,'Stations coordinates3.txt'));
for istation=1:size(stations,1)
name = stations.Station{istation};
network = stations.Network{istation};
lat = stations.Lat(istation);
lon = stations.Lon(istation);
[obsdata,txt,raw] = xlsread([obsdir,'\',network,'\', '2015','\','Airport.xlsx']);
end
  1 comentario
Stephen23
Stephen23 el 12 de En. de 2020
It is recommended to use fullfile rather than concatenating strings together. Instead of
[obsdir,'\',network,'\', '2015','\','Airport.xlsx']
just use fullfile like this (it automatically handles the file separator character):
fulfile(obsdir,network,'2015','Airport.xlsx')

Iniciar sesión para comentar.

Categorías

Más información sobre Manage Products en Help Center y File Exchange.

Etiquetas

Community Treasure Hunt

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

Start Hunting!

Translated by