How to access folders within a loop

2 Ansichten (letzte 30 Tage)
Daphne PARLIARI
Daphne PARLIARI am 11 Jan. 2020
Kommentiert: Stephen23 am 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??

Antworten (1)

Meg Noah
Meg Noah am 12 Jan. 2020
Bearbeitet: Meg Noah am 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 Kommentar
Stephen23
Stephen23 am 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')

Melden Sie sich an, um zu kommentieren.

Kategorien

Mehr zu Manage Products finden Sie in Help Center und File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by