Setting name of variable equal to name of imported file
10 次查看(过去 30 天)
显示 更早的评论
Hi,
I'm importing data into Matlab from excel via the following command:
cycle=xlsread('E:\DC.xlsx','Sheet1');
How can I set the name of the metadata to be the same as the name of the imported excel file.For example:
sch_metadata.name = 'DC';
It will be looping through files in a folder so I wont know the name in advance
Many thanks
0 个评论
回答(2 个)
Robert Cumming
2011-12-19
if you must do it then use dynamic fieldnames
metadata.(sch_metadata.name) = cycle
3 个评论
Robert Cumming
2011-12-19
same theory. TO get the filename use:
[fullpath, filename, ext] = fileparts ( 'E:\DC.xlsx' );
Then: sch_metadata.net = filename;
Matt Tearle
2011-12-19
If you're going to be looping on the files, you're probably doing something like
x = cellstr(ls('*.xlsx'));
for k = 1:length(x)
% do stuff
end
In the "do stuff" bit, you should use fileparts to pull apart each filename string:
[~,fn] = fileparts(x{k});
sch_metadata.net = fn;
save(fn);
% or save([fn,'.mat']) if you want to be explicit
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Spreadsheets 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!