How to import multiple variable starting the same from excel?

2 views (last 30 days)
Hi, I have an excel file with a lot of colums, and I would like to import all colums that starts similarely and store them in a new matrix like this :
W1_data= readtable(waffer1);
variable = W1_data.Variables;
for i = 1:22
Id = W1_data."DrainI_" + i + "_";
end
This solution does not work and I did not found how to do it.
Thank you

Answers (2)

dpb
dpb on 1 Aug 2022
Guessing from the above code snippet, try something like
W1_data= readtable(waffer1);
isDrainVar=startsWith(W1_data.Properties.VariableNames,'Drain');
W1_data=W1_data(:,isDrainVar);
will leave you with the table containing only those variables whos name begins with the string "Drain"

Benjamin Kraus
Benjamin Kraus on 1 Aug 2022
It is hard to offer detailed advice without more information about the structure of the Excel file, and what you mean "this solution does not work". Can you attach your Excel file to the post? What happened when you ran that code? Did you get an error message?
However, generically, my advice for people who want to learn how to import data from a spreadsheet is to use the Import Tool. That tool will show you a preview of your Excel file, and walk you through importing the data. It can even generate code so that you can automate the process later, and it can be very useful in learning the best way to import your specific data from an Excel file.

Community Treasure Hunt

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

Start Hunting!

Translated by