MATLAB: Load excel sheets with a specific name from the data set

data namingexcelloadmulti sheetsheetxlsread

Hey
First problem.
I have this problem where I want to load this excel sheet and store the first sheet as the name of sheet{1,1}(13,1) which in this example is: PP-244, (Ino I cant store a – sign, so Im looking for a way to seperate the string and the number from each other and then put them back together with a _ sign instead)
Second problem:
I can't find a way to load them into my workspace where they are stored with just the numbers in a matrix named as the sheet{1,1}(13,1).
I hope someone understands what im trying to do and can help me 🙂 If you could use the file I have attached it would be awesome, so I can see your script 🙂
Best Regards Mikkel

Best Answer

Hi Mikkel,
I do not know which data from the sheet you want to assign to the variable, but lets suppose you already have your data in this variable:
var_data = 'whatever'; % Variable containing the data
Now you can follow this steps:
  • Read the name on the sheet and modify it to fit the variable names restrictions:
[~,~,name] = xlsread('Test.xls',1,'A13'); % Read cell A13 from sheet 1 from Test.xls
name_split = strsplit(name{1},'-'); % Separate string and number
new_name = strcat(name_split(1),'_',name_split(2)); % Join string and number again
At this point you will have 'PP_217' instead of 'PP-217'.
  1. Generate a new variable with that name, and assign the value in your data variable:
var_name = genvarname(new_name);
eval([var_name '= var_data;']);
Now you will have what you are looking for.
Note that you can change 'var_data' with a command (i.e. if you want to read a matrix from the excel sheet). Also note that genvarname can be deprecated in your Matlab version, but there are replacements for the function.
I hope this helps!
Angel.