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

조회 수: 19 (최근 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