I have a set of 6 XY wavelength/absorption datasets, each with a different concentration. I have formed them into a 312x7 XYZ matrix with the first row holding the Y-variables (concentration), and the first column holding the X-variables (wavelength). I have used this data format quite frequently in other programs, but I cannot figure out how to get Matlab to recognize the data properly. I would like to use Matlab to baseline correct the data and then solve for the molar absorptivity at each wavelength.
Example:
0 10 25 50 75 100
290 0.0 0.1 0.12 0.15 0.2 0.3
291 0.0 0.11 0.121 0.151 0.21 0.31
Thanks in advance for your help.

 Akzeptierte Antwort

Cedric
Cedric am 15 Okt. 2013
Bearbeitet: Cedric am 15 Okt. 2013

2 Stimmen

EDIT: see my second solution for a more flexible approach.
=== 1st solution.
% - Read content.
fid = fopen( 'myFile.txt', 'r' ) ;
content = fscanf( fid, '%f', Inf ) ;
fclose( fid ) ;
% - Extact headers and data.
colHeader = content(1:6).' ;
content = reshape( content(7:end), 7, [] ).' ;
rowHeader = content(:,1) ;
data = content(:,2:end) ;
With that, you get..
>> colHeader
colHeader =
0 10 25 50 75 100
>> rowHeader
rowHeader =
290
291
>> data
data =
0 0.1000 0.1200 0.1500 0.2000 0.3000
0 0.1100 0.1210 0.1510 0.2100 0.3100
PS: there are other approaches, e.g. based on FGETL for the col. header and then TEXTSCAN, but I personnaly prefer reading the file in one shot and then reshaping/extracting numbers appropriately.
=== 2nd solution.
If the number of columns could vary among files though, I would go for a first step reading the first line of the file and determining dynamically the number of columns, before reading/processing the rest. For example:
fid = fopen( 'myFile.txt', 'r' ) ;
colHeader = sscanf( fgetl(fid), '%f' ).' ; % Read/convert 1st line only.
nDataCol = numel( colHeader ) ; % Determine # of data columns.
content = fscanf( fid, '%f', Inf ) ; % Read rest of the file.
fclose(fid) ;
content = reshape( content, nDataCol+1, [] ).' ;
rowHeader = content(:,1) ;
data = content(:,2:end) ;

6 Kommentare

Jan
Jan am 15 Okt. 2013
Bearbeitet: Jan am 15 Okt. 2013
@Cedric: "buffer" shadows the built-in function buffer. Although this command can solve some problems in the Matlab forums, I used it only one time for my real-world programs. So perhaps it is recommended to use another name, but I really do not know if the general rule is useful for rarely used functions.
Cedric
Cedric am 15 Okt. 2013
Bearbeitet: Cedric am 15 Okt. 2013
@Jan: you are right, it's a bad habit of mine actually (that I refuse to loose because the term is so generic that, AFAIK, it was a mistake to name this built-in this way), especially because I know the built-in! However, I shouldn't propagate my bad habits ;-), so I'll update my answer.
I wanted to start a discussion about conflicting function names between toolboxes/libs/etc by the way. I'd love to have something like .. well see Jan's new thread!
Soren
Soren am 15 Okt. 2013
Great! The data is in a .csv format. I think this is causing some issues in the parsing scheme.
% - Read content. fid = fopen( 'rawdata_array.csv', 'r' ) ; content = fscanf( fid, '%f', Inf ) ; fclose( fid ) ; % - Extact headers and data. colHeader = content(1:6).' ; buffer = reshape( content(7:end), 7, [] ).' ; rowHeader = buffer(:,1) ; data = buffer(:,2:end) ; Index exceeds matrix dimensions.
Thanks for your help in advance!
Cedric
Cedric am 15 Okt. 2013
Bearbeitet: Cedric am 15 Okt. 2013
So the separator is the comma and the first line is starting with a comma (empty content for 1st element)? If so, loading the data is much simpler:
content = csvread( 'rawdata_array.csv' ) ;
colHeader = content(1,2:end) ;
rowHeader = content(2:end,1) ;
data = content(2:end,2:end) ;
Soren
Soren am 16 Okt. 2013
Thank you very much for your quick and accurate assistance!
Soren
Cedric
Cedric am 16 Okt. 2013
You're welcome!

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (0)

Kategorien

Mehr zu Large Files and Big Data finden Sie in Hilfe-Center und File Exchange

Produkte

Gefragt:

am 15 Okt. 2013

Kommentiert:

am 16 Okt. 2013

Community Treasure Hunt

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

Start Hunting!

Translated by