How do I load variable sized cell arryays into Simulink matlab function?

In a Simulink embedded matlab function, I would like to read in a text file of strings and store them as a variable sized cell array. For my application, the size is unknown beforehand and determined only from the input file, so it cannot be initialized/pre-allocated without reading the file.
Input file "input.txt" would look something like this:
string1
string2
string3
and then my embedded matlab like this:
function states = getStates()
coder.extrinsic('textscan')
fid = fopen('input.txt','r'); % Read only
temp = textscan(fid,'%s'); % Store results into a cell array of cells (?)
states = temp{1}'; % Extract to just a cell array (?)
fclose(fid); % close file handle
This code does work as a standalone function in Matlab (it returns a 1x3 cell array of strings), but does not in Simulink's embedded matlab. I get the following error:
Cell contents reference from a non-cell array object.
Function 'getStates.m' (#555.221.228), line 5, column 10:
"temp{1}"
Launch diagnostic report.
I've also tried coder.varsize(states) which doesn't work
Any idea how to read in a variable sized cell array in Simulink?

2 commentaires

Are you using Matlab R2018a with string support in Simulink.
No, I'm currently using 2017b. Is this pretty straight forward to do in Matlab 2018a?

Connectez-vous pour commenter.

Réponses (0)

Produits

Version

R2017b

Question posée :

le 24 Mai 2018

Commenté :

le 25 Mai 2018

Community Treasure Hunt

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

Start Hunting!

Translated by