Discover MakerZone

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn more

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today

Thread Subject:
import data from txt file (containing blank rows and strings that should be skipped)

Subject: import data from txt file (containing blank rows and strings that should be skipped)

From: Benoît

Date: 8 Sep, 2011 21:23:25

Message: 1 of 3

Hi,

I can't figure out how to import my data from a txt file generated by another program :(
Here you can see a sample of how the txt looks like:

# AmiraMesh 3D ASCII 2.0


define VERTEX 819
define EDGE 1551
define POINT 8600

Parameters {
    ContentType "HxSpatialGraph"
}

VERTEX { float[3] VertexCoordinates } @1
EDGE { int[2] EdgeConnectivity } @2
EDGE { int NumEdgePoints } @3
POINT { float[3] EdgePointCoordinates } @4
POINT { float thickness } @5

# Data section follows
@1
6.377952098846436e-001 1.889763325452805e-001 4.724408686161041e-002
6.614173054695129e-001 2.047243863344193e-001 4.724408686161041e-002
6.692913174629211e-001 2.204724103212357e-001 7.874013483524323e-002
6.535432338714600e-001 2.047243863344193e-001 1.181101948022842e-001

@2
0 0
0 1

@3
7
4
4

@4
6.377952098846436e-001 1.889763325452805e-001 4.724408686161041e-002
6.353750228881836e-001 1.832907795906067e-001 4.724408686161041e-002
6.305581331253052e-001 1.797929853200913e-001 4.724408686161041e-002

@5
3.937007393687964e-003
3.937007393687964e-003


So what I'd like to do, is read the file in such a way that I get 1 matrix for every data section (@1, @2 etc...)
I thought about using a Perl filter and using textscan, but I'm not really familiar with Perl..
I tried applying a filter skipping strings and looking for a decimal value:
while (<>) { print if /^\s*\d/; }
but this didn't give me a good result at all :(

Can anybody help me with this problem?

grtz

Subject: import data from txt file (containing blank rows and strings that should be skipped)

From: Sadik

Date: 9 Sep, 2011 01:23:10

Message: 2 of 3

Hi Benoit,

One simple solution is in what follows.

Best.

----------------------------------------------------------------------------------------------------

fid = fopen('dataFile.txt');
counter = 0;
data = {};
while ~feof(fid)
    currentLine = fgetl(fid);
   
    if isempty(currentLine)
        continue;
    end
    
    % Skip the header part until we come across the first piece of data
    if counter == 0 && currentLine(1)~='@'
        continue;
    end

   % Check if new piece of data encountered.
    if currentLine(1)=='@'
        counter = counter+1;
        % Get ready to read the data.
        data{counter} = [];
    else
        % This is a true data line, read it.
        data{counter} = [data{counter}; sscanf(currentLine,'%f')'];
    end
end

fclose(fid);

Subject: import data from txt file (containing blank rows and strings that should be skipped)

From: Benoît

Date: 9 Sep, 2011 11:16:10

Message: 3 of 3

OMG, thank you so much!!!
Your solution is so elegant!
I just managed to do it myself in such an ugly way I wouldn't dare to put it up here...
Again, thank you very much!
It's working perfectly :-D


"Sadik " <sadik.hava@gmail.com> wrote in message <j4bppu$234$1@newscl01ah.mathworks.com>...
> Hi Benoit,
>
> One simple solution is in what follows.
>
> Best.
>
> ----------------------------------------------------------------------------------------------------
>
> fid = fopen('dataFile.txt');
> counter = 0;
> data = {};
> while ~feof(fid)
> currentLine = fgetl(fid);
>
> if isempty(currentLine)
> continue;
> end
>
> % Skip the header part until we come across the first piece of data
> if counter == 0 && currentLine(1)~='@'
> continue;
> end
>
> % Check if new piece of data encountered.
> if currentLine(1)=='@'
> counter = counter+1;
> % Get ready to read the data.
> data{counter} = [];
> else
> % This is a true data line, read it.
> data{counter} = [data{counter}; sscanf(currentLine,'%f')'];
> end
> end
>
> fclose(fid);

Tags for this Thread

No tags are associated with this thread.

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.

Contact us