How can I read structure components into Simulink?

11 vues (au cours des 30 derniers jours)
Emerson Butler
Emerson Butler le 12 Déc 2018
I have a data file which sends me 300+ variables (each variable is a column vector) which I want to access in Simulink. The data is structured in the following way:
foo bar baz alpha bet blah ...
12 6 and so on
18 15
72 8
... ...
Since there are so many variables that I want to have accessible in Simulink, I wanted to procedurally generate my variables somehow, where I'd have a column vector of foo = [12;18;72;...], bar = [6;15;8;...], for all of the variables. A quick google search reveals that procedurally generating variables is a big no-no. But, I found several other people asking about it and the general resolution is to read these values into a structure, where the field names are the first row and field values are the rest of the rows. I've done that, so now I have a structure with all 300+ variables, and to access them in Matlab I may type mystruct.bar to display the column of bar's values.
This works well in Matlab, but I can't find a good way to access these values in Simulink. I've tried using Constant blocks and From Workspace blocks, and each time I get an error, usually related to the variable not being found (mystruct.foo is what I'd put in as the Data name for example), or for the structure not having correct formatting. I've found a few places that say my structure needs to have a .time parameter, and a .singals structure which contains a .values parameter as well, but I'm not sure how I'd make that work with the way I have things formatted so far.
I was originally just creating each variable by searching the initial data file for the index of the word 'foo' and then setting foo equal to all the rows below that entry, but this is tedious and unrealistic considering how many variables there are. I don't necessarily have to have my data in a structure, but I would need a way to have all of my data read in from the data file somehow. The structure method seems to be the best way that I've found to achieve that goal, as a couple of for loops have created the full structure. Then the final problem is I need some way of accessing the data in simulink. With my original method of individually creating each variable, I would then load in that variable in through a Constant block, and then feed that variable into a Matlab function block which accepts a vector and outputs a single entry at a time for use in the simulation.
I'd appreciate any ideas on this issue, and I'm open to restructuring (or destructuring?) the way my data is read in and handled.

Réponse acceptée

Emerson Butler
Emerson Butler le 18 Déc 2018
A collegue pointed out to me that my structure was set up in such a way that the fields were cell vectors. Changing them to double vectors then allows me to use a Simulink "Constant" block, in which the constant would be mystruct.bar. I can then feed that into an indexing block and read each value one at a time in Simulink. It then looked a little like this:
[m,n] = size(CellArray); %Cell array held the field values
for index = 1:m
for index2 = 1:n
DoubleMatrix(index,index2) = double(string(CellArray{index,index2})); %Can't convert directly from cell to double
end
end
for index = 1:n
mystruct.(myFieldNames{index}) = DoubleMatrix(:,index);
end

Plus de réponses (0)

Catégories

En savoir plus sur Component-Based Modeling dans Help Center et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by