How to import data from excel file as structure into Matlab?

13 vues (au cours des 30 derniers jours)
Jannis Holtkoetter
Jannis Holtkoetter le 29 Oct 2020
Hello, I have a relatively straight forward problem:
I have an excel sheet with two columns. The first column contains names and the second column contains numeric values. How can I import this file as a struct, so that I can access the values simply by their name?
  2 commentaires
Walter Roberson
Walter Roberson le 29 Oct 2020
Are the values all the same datatype such as all numeric scalars?
Jannis Holtkoetter
Jannis Holtkoetter le 30 Oct 2020
Yes the values are all numeric scalars

Connectez-vous pour commenter.

Réponse acceptée

Walter Roberson
Walter Roberson le 30 Oct 2020
T = readtable(TheFileName);
TC = table2cell(T);
TS = cell2struct(TC(:,2), TC(:,1), 1);
TS will now be a cell with field names according to the first column of the file and numeric value according to the second column of the file.
  4 commentaires
vishal kulkarni
vishal kulkarni le 19 Avr 2022
If column has special charcater in it, it fails.
Like,
Param1.Field1 -> this will fail
Walter Roberson
Walter Roberson le 19 Avr 2022
Modifié(e) : Walter Roberson le 19 Avr 2022
User indicated that the second columns are all numeric scalars.

Connectez-vous pour commenter.

Plus de réponses (0)

Community Treasure Hunt

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

Start Hunting!

Translated by