Read Variables with assigned values from excel into Matlab

조회 수: 13 (최근 30일)
Ajiket Patil
Ajiket Patil 2021년 2월 4일
편집: Ajiket Patil 2021년 2월 4일
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.

채택된 답변

Stephen23
Stephen23 2021년 2월 4일
편집: Stephen23 2021년 2월 4일
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
Ajiket Patil
Ajiket Patil 2021년 2월 4일
Thanks for correcting the invalid variable name part.
My purpose is served by the above code.
Thank You very much @Stephen Cobeldick

댓글을 달려면 로그인하십시오.

추가 답변 (1개)

KSSV
KSSV 2021년 2월 4일
file = 'test.xlsx' ;
T = readtable(file) ;
vars = T.(2) ;
vars(isnan(vars))=[] ;
vars
  댓글 수: 6
KSSV
KSSV 2021년 2월 4일
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 2021년 2월 4일
편집: Ajiket Patil 2021년 2월 4일
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.

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Spreadsheets에 대해 자세히 알아보기

제품


릴리스

R2019a

Community Treasure Hunt

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

Start Hunting!

Translated by