load all the variables from .csv file in workspace as input to "From Workspace" block?

10 次查看(过去 30 天)
I have a simulink model in that am using From Workspace block to take input data from the workspace. Also i have a .csv file i need to load all the variables to the workspace for the model like (if the filename.csv file contains age, number and data.. i need to load each variables in the workspace)
I also used T = readtable(filename.csv) this is creating a T variable in the workspace, but i want to load all the variable( like age, number and etc...) not the T variable.
and also i use the import data option in the variable environment that is aslo not working for my need..
please help on this ..
Thanks in advance....

回答(1 个)

Tushar Behera
Tushar Behera 2023-2-3
Hi Muralidharan
I believe you want to import data from a excel file with their respective variable names such as age, number etc. instead of the whole table "T".
You can use the readtable function in MATLAB to load the data from the .csv file into the workspace and then extract the variables (e.g. age, number, etc.) you need using dot notation (e.g. T.age) or into separate variables using the following psuedocode:
T = readtable(filename.csv);
age = T.age;
number = T.number;
data = T.data;
This will load the variables age, number, and data into the workspace from the .csv file. Then you can use these variables as inputs in your Simulink model.
Hope this resolves your query.
Regards,
Tushar
  2 个评论
Sarah Gilmore
Sarah Gilmore 2023-2-3
Hi Muralidharan,
Tushar's answer works well, but you can also use the readvars function to do this with less code:
>> [age, number, data] = readvars(filename);
This will return the variables in filename as separate variables.
Best,
Sarah
Muralidharan
Muralidharan 2023-2-4
Thank you @Tushar Behera and @Sarah Gilmore for the answers.
Suppose if my .csv file containing 1000 variable and the variables or the order of the variables in the file changes everytime now how can i load all the variables into the workspace ??.
Please help me on this.

请先登录,再进行评论。

类别

Help CenterFile Exchange 中查找有关 Programmatic Model Editing 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by