Clear Filters
Clear Filters

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

14 views (last 30 days)
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....

Answers (1)

Tushar Behera
Tushar Behera on 3 Feb 2023
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 Comments
Sarah Gilmore
Sarah Gilmore on 3 Feb 2023
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 on 4 Feb 2023
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.

Sign in to comment.

Categories

Find more on Programmatic Model Editing in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!