Effacer les filtres
Effacer les filtres

Info

Cette question est clôturée. Rouvrir pour modifier ou répondre.

Importing excel coloumn as variables and their values

1 vue (au cours des 30 derniers jours)
Waqas Syed
Waqas Syed le 11 Fév 2016
Clôturé : MATLAB Answer Bot le 20 Août 2021
Hi,
I have simple excel file with two coloumns.
First coloumn has names e.g A,B,C,D etc Second coloumn has numeric values.
Number of components in both coloumns are equal.
I want to import both coloumns to matlab and create variables whose names are coloumn 1 and values are coloumn 2.
How can I do that?

Réponses (1)

Star Strider
Star Strider le 11 Fév 2016
I would use xlsread with two outputs:
[Num,Str] = xlsread( ... filename ...);
The numeric values will be in the ‘Num’ vector (in this instance), and the ‘Str’ variable will be a cell array of strings representing the first column.
Note — This is obviously UNTESTED CODE but it should work.
  2 commentaires
Waqas Syed
Waqas Syed le 11 Fév 2016
I dont think you understood my question. Sorry if I was not clear enough.
This would simply create two arrays.
I want every string of coloumn 1 to become a variable and get the value which is of the corresponding elecment in the second coloumn.
Star Strider
Star Strider le 11 Fév 2016
If you want to refer by name to every element in the cell column and get the corresponding numeric value, do this:
Num = randi(9, 10, 1); % Numeric Vector
Str = {'A'; 'B'; 'C'; 'D'; 'E'; 'F'; 'G'; 'H'; 'I'; 'J'}; % String (Cell) Vector
Val = Num(strcmp('C',Str)); % Return The Value In ‘Num’ Corresponding to ‘C’ In ‘Str’
Creating dynamic variables is very poor programming practice. It is much better to simply use a comparison such as I did here to retrieve the values you need.

Cette question est clôturée.

Community Treasure Hunt

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

Start Hunting!

Translated by