필터 지우기
필터 지우기

Importing Dates as variable names in an table from Excel

조회 수: 6 (최근 30일)
Josh Halff
Josh Halff 2023년 12월 8일
편집: Stephen23 2023년 12월 9일
Hello,
I'm trying to import data from Excel into MATLAB as a table, but I'm running into issues regarding variable labels.
The excel sheet is an automatically generated report with n rows representing various sites, and m columns. The first four columns give details of the site (location, site ID number, etc.) and then the rest of the columns represent dates from the time the tracking began to the day of the report. These columns are filled with 1s and 0s, ie 1 if I pinged that site that day and 0 if I didn't. I'd like to write this script in such a way that I can run the report and import it regardless of the date range I select for the report, so it needs to be very flexible.
Unfortunately, due to the variable difference between my pseudoboolean 1s and 0s and the date headers, when I import the data as a table, all my date columns assume a VarName5, VarName6, ...etc. variable name instead of the proper dates. This date information is important for the data manipulation I'm looking for. Does anyone know how to get the dates to come across as headers even if the variable type of the rest of each column is different?
Thanks!
Visual example of excel sheet:
Location IDNumber 3/1/2023 3/2/2023 3/3/2023 ...
LocationA ID_A 0 0 1
LocationB ID_B 1 1 1
LocationC ID_C 0 0 0
...
  댓글 수: 2
Walter Roberson
Walter Roberson 2023년 12월 8일
Are you using 'VariableNamingRule', 'preserve' ?
Josh Halff
Josh Halff 2023년 12월 8일
I've tried that, and had little luck. I've also realized that I made some significant mistakes that will prevent this from working as the dataset expands and I re-run...
I greatly appreciate your response. I'm going to try to rework, will update.

댓글을 달려면 로그인하십시오.

답변 (1개)

Sulaymon Eshkabilov
Sulaymon Eshkabilov 2023년 12월 9일
Here is one option that partially solves your exercise and explains it can't be solved completely:
T_VARS = readtable('VAR_SAVE.xlsx', ReadVariableNames=true,Range='A1:D5')
Warning: Column headers from the file were modified to make them valid MATLAB identifiers before creating variable names for the table. The original column headers are saved in the VariableDescriptions property.
Set 'VariableNamingRule' to 'preserve' to use the original column headers as table variable names.
T_VARS = 4×4 table
LOC IDNum x03_Mar_2023 x04_Mar_2023 ________ ________ ____________ ____________ {'LOCA'} {'LD_A'} 1 3 {'LOCB'} {'LD_C'} 2 5 {'LOCA'} {'LD_B'} 3 3.1322 {'LOCB'} {'CL_K'} 4 7
  댓글 수: 1
Stephen23
Stephen23 2023년 12월 9일
편집: Stephen23 2023년 12월 9일
"...and explains it can't be solved completely"
Reading and following the advice in the warning message:
T = readtable('VAR_SAVE.xlsx', 'ReadVariableNames',true, 'VariableNamingRule','preserve')
T = 4×7 table
LOC IDNum 03-Mar-2023 04-Mar-2023 05-Mar-2023 05-Mar-2023_1 07-Mar-2023 ________ ________ ___________ ___________ ___________ _____________ ___________ {'LOCA'} {'LD_A'} 1 3 10 0 1 {'LOCB'} {'LD_C'} 2 5 20 1 1 {'LOCA'} {'LD_B'} 3 3.1322 30 0 0 {'LOCB'} {'CL_K'} 4 7 40 1 0
Note that those dates are store in the excel as Excel serial date numbers (days since 1 Jan 1900), apparently the formatting is not stored for each cell.
Also note that columns E and F contain identical headers, which READTABLE must modify to be unique.

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Spreadsheets에 대해 자세히 알아보기

제품


릴리스

R2022a

Community Treasure Hunt

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

Start Hunting!

Translated by