MATLAB: Is there a way to directly load the data from a text file into a S-function in Simulink 7.0 (R2007b)

simulink

I have a text file with data and I want to load this data directly into an S-function.

Best Answer

Below are the steps that you can use to read data directly from a text file into Simulink S-function as a source:
1. Specify the sizes and the sample time for the S-function.
2. Open the file by using the FOPEN function in the mdlStart or the mdlInitializeConditions function.
3. Store the pointer in a PWork vector for later use.
4. In the mdlOutputs function, obtain the file pointer from work storage and use the FSCANF to read the next element.
5. Then close the file using the FCLOSE in mdlTerminate.
See the attached simple example model 'test_textSource.mdl' which reads the data from a text file 'input.txt' into an S-function.