Read Excel File into code
5 views (last 30 days)
Show older comments
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!
Answers (1)
hector martinez
on 14 Nov 2019
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.
See Also
Categories
Find more on Spreadsheets in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!