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

19 visualizaciones (últimos 30 días)
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....

Respuestas (1)

Tushar Behera
Tushar Behera el 3 de Feb. de 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 comentarios
Sarah Gilmore
Sarah Gilmore el 3 de Feb. de 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 el 4 de Feb. de 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.

Iniciar sesión para comentar.

Community Treasure Hunt

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

Start Hunting!

Translated by