How to access folders within a loop

2 ビュー (過去 30 日間)
Daphne PARLIARI
Daphne PARLIARI 2020 年 1 月 11 日
コメント済み: Stephen23 2020 年 1 月 12 日
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??

回答 (1 件)

Meg Noah
Meg Noah 2020 年 1 月 12 日
編集済み: Meg Noah 2020 年 1 月 12 日
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 件のコメント
Stephen23
Stephen23 2020 年 1 月 12 日
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')

サインインしてコメントする。

カテゴリ

Help Center および File ExchangeCharacters and Strings についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by