How do you write a filepath into a table?

5 visualizzazioni (ultimi 30 giorni)
I have searched for some answer relevent to what I am asking and cannot find anything, so any help would be appreciated. I have a strange set of data, in that the filename of the .csv does not give all of the info of what is in the data, most of the info is located in the folder names. This is not an issue when dealing with a small amount, and I can look at it manually. However I have hundreds of filepaths. So my question is, is there a way to put the data in a table with information from the folder names in the table with it? for example if the path was something like
C:\Users\JY\Desktop\Weather\Monday\Overcast.csv
can I have a table that would show
[Weather,Monday,Overcast,"data_from_.csv" ]
thanks in advance for any help on this.

Risposta accettata

Voss
Voss il 4 Mar 2022
file_names = { ...
'C:\Users\JY\Desktop\Weather\Monday\Overcast.csv'
'C:\Users\JY\Desktop\Weather\Tuesday\Sunny.csv' ...
};
% split the file names on '\' and '.' (you can use filesep() instead of '\'
% here):
new_names = cellfun(@(x)strsplit(x,{'\' '.'}),file_names,'UniformOutput',false);
new_names{:}
ans = 1×8 cell array
{'C:'} {'Users'} {'JY'} {'Desktop'} {'Weather'} {'Monday'} {'Overcast'} {'csv'}
ans = 1×8 cell array
{'C:'} {'Users'} {'JY'} {'Desktop'} {'Weather'} {'Tuesday'} {'Sunny'} {'csv'}
% Keep the last 4 parts (everything after "Desktop"):
file_info = cellfun(@(x)x(end-3:end),new_names,'UniformOutput',false);
file_info = vertcat(file_info{:})
file_info = 2×4 cell array
{'Weather'} {'Monday' } {'Overcast'} {'csv'} {'Weather'} {'Tuesday'} {'Sunny' } {'csv'}
% append the 'data_from_.' to the last column (containing 'csv'):
file_info(:,end) = strcat('data_from_.',file_info(:,end));
disp(file_info);
{'Weather'} {'Monday' } {'Overcast'} {'data_from_.csv'} {'Weather'} {'Tuesday'} {'Sunny' } {'data_from_.csv'}
% convert to an array of strings if that's what you need to use:
file_info_str = string(file_info);
disp(file_info_str);
"Weather" "Monday" "Overcast" "data_from_.csv" "Weather" "Tuesday" "Sunny" "data_from_.csv"
% or convert either one into a table:
t = array2table(file_info,'VariableNames',{'Type','Day','Category','file(?)'})
t = 2×4 table
Type Day Category file(?) ___________ ___________ ____________ __________________ {'Weather'} {'Monday' } {'Overcast'} {'data_from_.csv'} {'Weather'} {'Tuesday'} {'Sunny' } {'data_from_.csv'}
t = array2table(file_info_str,'VariableNames',{'Type','Day','Category','file(?)'})
t = 2×4 table
Type Day Category file(?) _________ _________ __________ ________________ "Weather" "Monday" "Overcast" "data_from_.csv" "Weather" "Tuesday" "Sunny" "data_from_.csv"

Più risposte (0)

Categorie

Scopri di più su MATLAB in Help Center e File Exchange

Prodotti


Release

R2021b

Community Treasure Hunt

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

Start Hunting!

Translated by