Creating variables and assigning values from table data

18 vues (au cours des 30 derniers jours)
Mihir Tasgaonkar
Mihir Tasgaonkar le 15 Déc 2022
Hi,
I'm looking to create variables and save them in a .mat file. The issue is all the variable names are stored in an Excel file along with their values (variable name in Column A and its value in Column B). Is there any way I can create these variables directly from the table data? I have almost 1500 rows in the Excel file, so doing this manually ( like A = B(1,1)) is not a option.
Please help out.

Réponses (1)

Stephen23
Stephen23 le 15 Déc 2022
"Is there any way I can create these variables directly from the table data?"
Easy, just convert the data to a structure and SAVE using the -STRUCT option. This shows you the basic idea:
C = readcell('test.csv')
C = 2×2 cell array
{'hello'} {[3.1415]} {'world'} {[ 123]}
S = cell2struct(C(:,2),C(:,1),1)
S = struct with fields:
hello: 3.1415 world: 123
save('test.mat','-struct','S')

Produits


Version

R2021b

Community Treasure Hunt

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

Start Hunting!

Translated by