Auto-naming a variable based on imported file name

7 ビュー (過去 30 日間)
Jessica Towns
Jessica Towns 2021 年 6 月 23 日
回答済み: Steven Lord 2021 年 6 月 23 日
Hi everyone,
I'm writing a code where I need to import data from specific cells in an excel file each time I want to run the code. I have to create a table of this specified data, but don't want to hard code the range of the excel file each time since the column I am pulling from is always different. I'm wondering if there is a way to automatically name the table, based on the name of the file that I import. The line of code I am currently using is
table = CalendarS4.StartTime25;
where it outputs a table of strings from the selected cells of the excel sheet. The data will always be from CalendarS4.StartTime, but the number following 'StartTime' is different based on the column I am importing data from. Is there a way to automatically name the table after the file name, without having to change the column number at the end each time? I am trying to run the program without having to change the code to match the file name.
Thanks in advance!

回答 (1 件)

Steven Lord
Steven Lord 2021 年 6 月 23 日
Can you do this? Sometimes. File names are not required to be valid MATLAB identifiers. I can have a file named "3 blind mice.txt" but that violates two of the rules for valid MATLAB identifiers given in the documentation for the isvarname function.
Should you do this? The general consensus is no.

カテゴリ

Help Center および File ExchangeSpreadsheets についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by