Read Variables with assigned values from excel into Matlab

I want to read variables with assigned values from excel file (in multiple sheets) into Matlab. In the below snip Var1, Var2 and Var 3 have assigned values 10,5 and 21 respectively.
Kindly inform if it is possible. Thank You.

 Réponse acceptée

Stephen23
Stephen23 le 4 Fév 2021
Modifié(e) : Stephen23 le 4 Fév 2021
Note how the file itself contains invalid variable names: they contain space characters which in all of your comments you have simply ignored. Computers however do not just ignore things in the way that humans like to do, and this hints at some of the reasons why some issues with this approach (how to handle invalid names? How to handle conflicting/identical names? etc.).
Here is one simple method which creates an easy-to-use table of the imported data:
tbl = readtable('test.xlsx','ReadRowNames',true);
tbl = rows2vars(tbl,'VariableNamingRule','modify')
tbl = 1x8 table
OriginalVariableNames Var1 Var2 Var3 Row4 INPUTSOFTYPE2 Var4 Var5 _____________________ ____ ____ ____ ____ _____________ ____ ____ {'Var1'} 10 5 21 NaN NaN 21 4
And accessing the values:
tbl.Var1
ans = 10
tbl.Var5
ans = 4

1 commentaire

Thanks for correcting the invalid variable name part.
My purpose is served by the above code.
Thank You very much @Stephen Cobeldick

Connectez-vous pour commenter.

Plus de réponses (1)

file = 'test.xlsx' ;
T = readtable(file) ;
vars = T.(2) ;
vars(isnan(vars))=[] ;
vars

6 commentaires

@KSSV I want to assign variables Var1 = 10, Var2 = 5 .... in the excel sheet to the workspace in Matlab i.e (read assigned inputs with variable values from an excel sheet and use in matlab). In matlab workspace
In matlab workspace, i need to read both variable name along with assigned value.
The Workspace should contain.
Var1 = 10,
Var2 = 5
Var3 = 21 and so on.
The given code exactly does what you want.
vars(1) % this is vars1
vars(3) % this is vars3
.
.
.
vars(i)
It gives vars as a 5*1 array. I want workspace with assigned double variables.
Eg. Workspace should look like
Var1 = 10;
Var2 = 5;
Var3 = 21;
Var4 = 21;
Var5 = 4;
My purpose is not to remember eg. vars(3) = Var3, while performing computations with the variables, as i will deal with multiple variables.
clarification if variable names are confusing.
The output of the code should be in workspace
a = 10;
b = 5;
c = 21;
d = 21;
e = 4;
and not an vars (5*1) array, to remember vars(3) = c.
Thank you for your effort.
I got the question...my point is there is no meaning in assigning the variables like you want. It is not required.
Ajiket Patil
Ajiket Patil le 4 Fév 2021
Modifié(e) : Ajiket Patil le 4 Fév 2021
I am making an app through matlab app designer. It reads data in the above format and performs further computations.
I require as per the above format because of not to remember vars(3) = c. as i am dealing in many variables. Noting the cell location in the excel sheet for all the variables is tedius.
Thank You.

Connectez-vous pour commenter.

Produits

Version

R2019a

Community Treasure Hunt

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

Start Hunting!

Translated by