How can I change a variable's name to an excel sheet name?

I'm sure there is an easy answer to this, but I haven't used Matlab in a while and am a bit rusty.
I am wanting to import data from an excel file with multiple sheets, then name each variable the sheet name. Here's what I have so far, but maybe there is a better way...
[STATUS, SHEETS] = xlsfinfo('022117_EIS_Data.xls'); % Use user selected info
isempty = zeros(61,5);
for i = 1 : max(size(SHEETS))
isempty = xlsread('022117_EIS_Data.xls',i);
end
Basically I was hoping to rename 'isempty' every loop as the current excel sheet (saved in SHEETS)

1 Comment

Naming variables dynamically is one way that beginners force themselves into writing slow, complex, buggy code that is hard to debug. Read this to know why:
If the sheets names are significant then this means that they are contain meta-data, and meta-data should simply be stored as data in their own right (e.g. in a cell array.

Sign in to comment.

 Accepted Answer

Image Analyst
Image Analyst on 17 May 2018
Edited: Image Analyst on 17 May 2018
Don't do that. isempty() is a built-in function. There is no way you should override this function with anything.
Nor do you want to create variables with names derived/determined by some cell contents of the workbook. How would you know how to reference those variables later in your program if you don't know their name until runtime? This is a bad idea.

1 Comment

Ya realized 'isempty' was the wrong choice of variable. I've changed it. Thanks.
And sounds good. I'll just work with them as a 3D array. I am just wanting the Sheet names to be 'tags' for each dataset, but when outputting my result, I should just be able to call to the corresponding sheet.

Sign in to comment.

More Answers (0)

Community Treasure Hunt

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

Start Hunting!