Filter löschen
Filter löschen

Creating variables and assigning values from table data

15 Ansichten (letzte 30 Tage)
Mihir Tasgaonkar
Mihir Tasgaonkar am 15 Dez. 2022
Beantwortet: Stephen23 am 15 Dez. 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.

Antworten (1)

Stephen23
Stephen23 am 15 Dez. 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')

Produkte


Version

R2021b

Community Treasure Hunt

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

Start Hunting!

Translated by