How to access folders within a loop

3 vues (au cours des 30 derniers jours)
Daphne PARLIARI
Daphne PARLIARI le 11 Jan 2020
Commenté : Stephen23 le 12 Jan 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??

Réponses (1)

Meg Noah
Meg Noah le 12 Jan 2020
Modifié(e) : Meg Noah le 12 Jan 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 commentaire
Stephen23
Stephen23 le 12 Jan 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')

Connectez-vous pour commenter.

Catégories

En savoir plus sur Manage Products dans Help Center et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by