Combining Spreadsheets to Create a Table in Matlab
Mostra commenti meno recenti
Hello,
I have 4 spreadsheets, which have different data for zip codes. I want to create one spreadsheet for each zip code, where there the data from each spreadsheet is represented in the column for the zip code's own spreadsheet.
For instance, I have these spreadsheets, "WeeklyBreakZip.xlsx", "WeeklyManholeZip.xlsx", "WeeklyCatchZip.xlsx" and "WeeklyBackupZip.xlsx", "WeeklyStreetZip.xlsx" .
Each spreadsheet has zipcodes as the column headers, with the data listed. For example, one Zip Code is 10010.
For each Zip Code, I want to extract the columns from the four worksheets, and create its own worksheet.
So, for example, with zip code 10010, the columns of this new compiled worksheet would be:
Catch Back Break Manhole Street
Then, the data would be listed below.
How would I get Matlab to do this?Thank you. I very much appreciate any help.
2 Commenti
bharath pro
il 29 Giu 2020
Can there be more than the 4 spreadsheets given in the question?
CMatlabWold
il 29 Giu 2020
Risposta accettata
Più risposte (1)
Cris LaPierre
il 29 Giu 2020
There are a couple things that make this problem challenging
- MATLAB does not like having variable names be numbers (the zip codes)
- Different amounts of data are recorded in your spreadsheets, meaning you need to have a plan for how to handle missing/extra zip codes between the data.
- You have an extra table in WeeklyStreetZip.xslx (two tables with 530 rows).
Forgive the approach here, but it's what I had to do to get something that worked. Feel free to modify. It does take a while to run.
BackupZip = readtable("WeeklyBackupZip.xlsx","Range",'1:1',"ReadRowNames",true,"ReadVariableNames",true,"PreserveVariableNames",true);
BackupData = readtable("WeeklyBackupZip.xlsx","NumHeaderLines",1,"ReadRowNames",true);
BackupData.Properties.VariableNames = BackupZip.Properties.VariableNames;
BreakZip = readtable("WeeklyBreakZip.xlsx","Range",'1:1',"ReadRowNames",true,"ReadVariableNames",true,"PreserveVariableNames",true);
BreakData = readtable("WeeklyBreakZip.xlsx","NumHeaderLines",1,"ReadRowNames",true);
BreakData.Properties.VariableNames = BreakZip.Properties.VariableNames;
CatchZip = readtable("WeeklyCatchZip.xlsx","Range",'1:1',"ReadRowNames",true,"ReadVariableNames",true,"PreserveVariableNames",true);
CatchData = readtable("WeeklyCatchZip.xlsx","NumHeaderLines",1,"ReadRowNames",true);
CatchData.Properties.VariableNames = CatchZip.Properties.VariableNames;
ManholeZip = readtable("WeeklyManholeZip.xlsx","Range",'1:1',"ReadRowNames",true,"ReadVariableNames",true,"PreserveVariableNames",true);
ManholeData = readtable("WeeklyManholeZip.xlsx","NumHeaderLines",1,"ReadRowNames",true);
ManholeData.Properties.VariableNames = ManholeZip.Properties.VariableNames;
StreetZip = readtable("WeeklyStreetZip.xlsx","Range",'1:1',"ReadRowNames",true,"ReadVariableNames",true,"PreserveVariableNames",true);
StreetData = readtable("WeeklyStreetZip.xlsx","NumHeaderLines",1,"ReadRowNames",true);
StreetData = StreetData(1:height(BackupData),:);
StreetData.Properties.RowNames = BackupData.Properties.RowNames;
StreetData.Properties.VariableNames = StreetZip.Properties.VariableNames;
zips = categorical([BackupZip{:,:},BreakZip{:,:},CatchZip{:,:},ManholeZip{:,:},StreetZip{:,:}]);
zips = categories(zips);
r = BackupData.Properties.RowNames;
for z = 1:length(zips)
try
Backup = BackupData{r,zips(z)};
catch
Backup = NaN([height(BackupData),1]);
end
try
Break = BreakData{r,zips(z)};
catch
Break = NaN([height(BreakData),1]);
end
try
Catch = CatchData{r,zips(z)};
catch
Catch = NaN([height(CatchData),1]);
end
try
Manhole = ManholeData{r,zips(z)};
catch
Manhole = NaN([height(ManholeData),1]);
end
try
Street = StreetData{r,zips(z)};
catch
Street = NaN([height(StreetData),1]);
end
zipTable = table(Backup,Break,Catch,Manhole,Street,'RowNames',r);
writetable(zipTable,"WeeklyZipData.xlsx","Sheet",string(BackupZip{1,z}),"WriteRowNames",true);
end
3 Commenti
CMatlabWold
il 30 Giu 2020
Cris LaPierre
il 30 Giu 2020
That is a newer setting, so if you are using an older versino of MATLAB, it is likely it's not available.
CMatlabWold
il 2 Lug 2020
Categorie
Scopri di più su Spreadsheets in Centro assistenza e File Exchange
Prodotti
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!