Setting name of variable equal to name of imported file

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

답변 (2개)

Robert Cumming
Robert Cumming 2011년 12월 19일
if you must do it then use dynamic fieldnames
metadata.(sch_metadata.name) = cycle

댓글 수: 3

Thanks for the reply,
Maybe I didn't describe it very well, I want to save it with the same as the imported excel file, not as the variable cycle.
Would you suggest the same in this case?
Many thanks
Full code:
sch_cycle=xlsread('E:\DC.xlsx','Sheet1');
nrows = size(cycle,1);
sch_grade=[0,0;nrows,0];
sch_key_on=[0 1; nrows 1];
sch_metadata.net = 'DC';
sch_metadata.proprietary='public';
save DC.mat
same theory. TO get the filename use:
[fullpath, filename, ext] = fileparts ( 'E:\DC.xlsx' );
Then: sch_metadata.net = filename;
Thanks very much

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

Matt Tearle
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

카테고리

도움말 센터File Exchange에서 Data Import from MATLAB에 대해 자세히 알아보기

태그

질문:

2011년 12월 19일

Community Treasure Hunt

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

Start Hunting!

Translated by