Read Excel File into code

조회 수: 5 (최근 30일)
Robyn Macartney
Robyn Macartney 2019년 11월 14일
댓글: Robyn Macartney 2019년 11월 14일
Hi I have created code to determine a true/false response based on multiple user input data which is stored in an excel file. Is there a way I can write some code to get the data within the excel file to be entered into the MatLab inputs?
I have attempted using csvread function and can't get this to work. Any suggestions welcome, thank you!
  댓글 수: 1
Alex Mcaulley
Alex Mcaulley 2019년 11월 14일
Have you tried with xlsread?

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

답변 (1개)

hector martinez
hector martinez 2019년 11월 14일
Csvread will only read an excel file with numeric values. If your values aren't numeric or if you have column names that aren't numeric, it won't work. You can get around this by skipping the first row of your data so it won't read your column headers.
csvResponse = csvread('csvFile',1,0)
and changing your true/false values to 1/0.
I prefer using readtable, because your data can be any type. Additionally, column headers can be used as variables, and you can use dot notation to access values in the table.
tableResponse = readtable('csvFile');
% Replace Column1 with the name of whatever column your trying to access, x is the index of the value you want.
someResponse = tableResponse.Column1(x)
My favorite feature is being able to gather data from one column based on values from a different column, such as:
anotherResponse = tableResponse.Column1(tableResponse.Column2 == someCondition)
This says get the values from Column1 where the values in Column2 equal someCondition.
  댓글 수: 1
Robyn Macartney
Robyn Macartney 2019년 11월 14일
Thanks, I managed to get my file to be read into the code as a matrix using readmatrix function. This matrix is 13 columns of variables for a number of subjects, is there a way I would be able to write code to enable each row to be read and the variables entered into my condition statements? And repeat for each subject case?

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

카테고리

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

Community Treasure Hunt

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

Start Hunting!

Translated by