load excel sheets with a specific name from the data set

7 visualizaciones (últimos 30 días)
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

Respuesta aceptada

Angel Torrado-Carvajal
Angel Torrado-Carvajal el 24 de Ag. de 2017
Editada: Angel Torrado-Carvajal el 25 de Ag. de 2017
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.
  2 comentarios
Mikkel Ibsen
Mikkel Ibsen el 25 de Ag. de 2017
Editada: Mikkel Ibsen el 25 de Ag. de 2017
If I use the first second code you wrote:
name = xlsread('Test.xls',1,'A13'); % Read cell A13 from sheet 1 from Test.xls
Then it gives me an empty []. However if I use:
[~,~,name] = xlsread('Test.xls',1,'A13');
It gives me the right value, but is a cell.
But then when I try to run the next line it says:
Error using strsplit (line 80)
First input must be either a character vector or a string scalar.
The "name" variable is a 1x1 cell.
What can I do to make this work?
Angel Torrado-Carvajal
Angel Torrado-Carvajal el 25 de Ag. de 2017
Hi Mikkel,
I am sorry for the errors, I had to write the previous answer without having access to Matlab. I updated my previous answer with the corrected code. As you said, xlsread returns three variables:
[num,txt,raw] = xlsread(___)
It actually returns the numeric data in a matrix num, the text fields in cell array txt, and both numeric and text data in cell array raw.
As you have your data in a cell array, you can access the actual string by accessing the first field in the cell array {1}.
name_split = strsplit(name{1},'-');
I hope it works now!

Iniciar sesión para comentar.

Más respuestas (0)

Community Treasure Hunt

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

Start Hunting!

Translated by