Thread Subject:
Read a particular line from a text file in Embedded Matlab (inside Simulink)

Subject: Read a particular line from a text file in Embedded Matlab (inside Simulink)

From: Tiger

Date: 26 Jun, 2012 11:10:07

Message: 1 of 4

Hi,

Here is a test text file content I'm dealing with:
1 2 3 4 5 6 7
2 4 6 3 6 7 4
6 3 5 3 6 7 4
5 6 7 3 6 3 4
...

How can I read one particular line at a time inside Embedded Matlab function. I've developed the following code inside an Embedded Matlab function in SimEvents:

function Task = fcn(Count) % Count is the number of the specific line I want to extarct/read
Task = zeros(1, 1);
C = zeros(1, 1);
eml.extrinsic('fopen', 'textread', 'fclose')
fid = fopen('file.txt');
C = textread('file.txt', '%s', 'delimiter', '\n');
Task = C(Count);
fclose(fid)

I get the following errors:
1. Embedded MATLAB Interface Error: MATLAB expression 'textread' is not numeric. This error highlights the whole Embedded Matlab function block.
2. I used get the following error: Subscripting into an mxArray is not supported. I think I solved it by setting the size of Task and C in the beginning. However, I'm not sure whether I need to declare both or only Task in the beginning.

I've tried 'textscan' also, and it has the same errors above.

looking forward your comments, cheers.
____________
Tiger

Subject: Read a particular line from a text file in Embedded Matlab (inside Simulink)

From: Tiger

Date: 26 Jun, 2012 11:24:07

Message: 2 of 4

Hi again,

Just to make one thing clear, the variable Count in the above code is an integer (1, 2, 3, etc.) and I use it to specify the nth line to read not how many lines to extract.

Subject: Read a particular line from a text file in Embedded Matlab (inside Simulink)

From: Tiger

Date: 26 Jun, 2012 11:33:07

Message: 3 of 4

Also, lines might change in size as it's a big text file I'm dealing with. At some instances I might have a field with "6,7" insrtead of one individual integer value.

Many thanks in advance you guys.

Subject: Read a particular line from a text file in Embedded Matlab (inside Simulink)

From: Phil Goddard

Date: 26 Jun, 2012 13:33:07

Message: 4 of 4

Your first step needs to be to sort out sizes and datatypes.

Here you are telling MATLAB that Task and C are going to be numerical scalars
> Task = zeros(1, 1);
> C = zeros(1, 1);

Yet here you are trying to put character strings of undetermined length into them
> C = textread('file.txt', '%s', 'delimiter', '\n');
> Task = C(Count);

That is never going to work with Embedded MATLAB.

Also, I'd suggest that opening and closing a file every time the block is called is going to be incredibly inefficient.
You'd be better to read the file before starting the simulation (doing any data conversion at this time) and then just working with numeric values in the code.

Phil.

Tags for this Thread

Everyone's Tags:

Add a New Tag:

Separated by commas
Ex.: root locus, bode

What are tags?

A tag is like a keyword or category label associated with each thread. Tags make it easier for you to find threads of interest.

Anyone can tag a thread. Tags are public and visible to everyone.

Tag Activity for This Thread
Tag Applied By Date/Time
specific lines Tiger 26 Jun, 2012 07:14:09
text files Tiger 26 Jun, 2012 07:14:09
embedded matlab fu... Tiger 26 Jun, 2012 07:14:09
rssFeed for this Thread

Contact us