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:
Reading Non-Rectangular Text File

Subject: Reading Non-Rectangular Text File

From: Brian B

Date: 23 Jul, 2010 00:45:05

Message: 1 of 5

Hello, I'm trying to read a text file that has no column or row structure, here's a sample:
[code]
 Configuration:
     # Surfaces = 6
     # Strips = 160
     # Vortices =1600

  Sref = 11.511 Cref = 0.0000 Bref = 256211
  Xref = 13.667 Yref = 0.0000 Zref = 0.0000
 
  Standard axis orientation, X fwd, Z down
  
  Alpha = 0.00000 pb/2V = 0.00000 p'b/2V = 0.00000
  Beta = 0.00000 qc/2V = 0.00000
  Mach = 0.000 rb/2V = 0.00000 r'b/2V = 0.00000

  CXtot = -0.22222 Cltot = 0.00000 Cl'tot = 0.11110
[/code]

What I'd like to do is search for the equal signs in the text file and the use the string preceding the equal sign as the variable name and value succeeding the equal sign as the respective value for that variable.

So far I've been playing around with functions: fileread, strfind(text,' = '), and textscan . If I stared at the help file long enough I'm sure I could find an inefficient way to capture the value after the equal sign, but I'm not sure how to assign it to its respective variable name.

Thanks for your help!

-Brian

Subject: Reading Non-Rectangular Text File

From: Brian B

Date: 23 Jul, 2010 16:13:26

Message: 2 of 5

Any advice?

Subject: Reading Non-Rectangular Text File

From: Andres

Date: 24 Jul, 2010 21:30:06

Message: 3 of 5

Once you have extracted the right strings before and after each equal sign, you'll need
 genvarname
to generate valid variable names from the 'before'-strings,
 sscanf
to convert the 'after'-strings to numeric data, and finally
 assignin
to get your variables assigned.

Subject: Reading Non-Rectangular Text File

From: Jan Simon

Date: 24 Jul, 2010 21:56:03

Message: 4 of 5

Dear Brian,

> [code]
> Configuration:
> # Surfaces = 6
> # Strips = 160
> # Vortices =1600
>
> Sref = 11.511 Cref = 0.0000 Bref = 256211
> Xref = 13.667 Yref = 0.0000 Zref = 0.0000
>
> Standard axis orientation, X fwd, Z down
>
> Alpha = 0.00000 pb/2V = 0.00000 p'b/2V = 0.00000
> Beta = 0.00000 qc/2V = 0.00000
> Mach = 0.000 rb/2V = 0.00000 r'b/2V = 0.00000
>
> CXtot = -0.22222 Cltot = 0.00000 Cl'tot = 0.11110
> [/code]

Please contact the author of the file, that it is rather complex to parse such files automatically. It is not well structured, although it looks like it would be.
Parsing such strings cannot be smart or nice.

I'd suggest to avoid using the names generated by GENVARNAME as names of variables. After parsing your example, you cannot access the Matlab function BETA anymore! Better use a struct and dynamic field names to keep your workspace clean.

Good luck, Jan

Subject: Reading Non-Rectangular Text File

From: Andres

Date: 24 Jul, 2010 22:11:03

Message: 5 of 5

"Jan Simon" <matlab.THIS_YEAR@nMINUSsimon.de> wrote in message <i2fnhj$m97$1@fred.mathworks.com>...
[..]
> I'd suggest to avoid using the names generated by GENVARNAME as names of variables. After parsing your example, you cannot access the Matlab function BETA anymore! Better use a struct and dynamic field names to keep your workspace clean.

Good argument. At least you could use genvarname to generate the field names.

Tags for 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