Creating variables and assigning values from table data

21 ビュー (過去 30 日間)
Mihir Tasgaonkar
Mihir Tasgaonkar 2022 年 12 月 15 日
回答済み: Stephen23 2022 年 12 月 15 日
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.

回答 (1 件)

Stephen23
Stephen23 2022 年 12 月 15 日
"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')

カテゴリ

Help Center および File ExchangeSpreadsheets についてさらに検索

製品


リリース

R2021b

Community Treasure Hunt

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

Start Hunting!

Translated by