Is there an easy way to read a structure,e.g. file header, from a file into
Matlab?
regards
Hailin
It depends. What file format are you trying to read? If you're reading in
a plain text file, DLMREAD or TEXTREAD may help you out. Excel file? See
XLSREAD. Binary file for which you know the format? FSCANF and the
low-level I/O functions listed in the "Formatted file I/O" section of HELP
IOFUN will be useful. [Binary file for which you _don't_ know the format?
That's tricky.]
I'd suggest if none of these work to look through HELP IOFUN and see if one
of the functions listed therein works on your file format.
--
Steve Lord
sl...@mathworks.com
-Alex Dannenberg
Al...@PineMountainCapital.com
"Steven Lord" <sl...@mathworks.com> wrote in message
news:ap17vq$fmt$1...@news.mathworks.com...
Here is a function that will read data from a file into a structure. The
rows of the file must be field and subfield names with the last entry of the
row being a value. For example, if the rows of myfile.txt file are:
test1 3.141
test2 test3 'pi'
test4 pi
The the command below will give the result shown:
>> s = readstruct('myfile.txt')
s =
test1: 3.141
test2.test3: 'pi'
test4: 'pi'
I hope this might be useful to you.
Regards,
Ken
Here's the function:
function s = readStruct(file)
% READSTRUCT(FILE) Reads structure from text file, FILE.
% The last entry in each row is the value. The previous
% entries are field (and subfield) names.
% Open the specified file.
fid = fopen(file);
% Read in the first line and set the line number.
line = fgetl(fid);
% The function, fgetl, returns a -1 when an end-of-file is encountered.
while ~isequal(line, -1);
% Initially, no items from this line
items = {};
% Number of items extracted from this line.
numberOfItems = 0;
% While there is still some path left, keep parsing nodes into a cell
array.
while line
numberOfItems = numberOfItems + 1;
[items{numberOfItems}, line] = strtok(line);
end
% Only process non-empty lines.
if numberOfItems > 1
% Last item on line is value of field. If first element of last item
is a letter,
% then assume that last item is a char array. Otherwise, assume it
is a number.
if isletter(items{end}(1))
value = ['''', items{end}, '''']; % Wrap quotes around string.
else
value = items{end};
end
% remove value from list of items (leaving only field names).
items = items(1:length(items)-1);
fieldname = 's'; % Initialize construction of field name.
for item = items
fieldname = [fieldname, '.', item{:}]; % Append subfield names
to field name.
end
% Construct command to assign value to field name.
command = [fieldname, ' = ', value, ';'];
eval(command);
elseif numberOfItems == 1;
error ('Every non-empty line must contain must contain at least one
field name and one value.')
end
line = fgetl(fid);
end
fclose(fid);
>Dear all,
>
>Is there an easy way to read a structure,e.g. file header, from a file into
>Matlab?
It most certainly depends on the structure :-) of the file. If the
structure is complex the command to read the file will probably be
complex too.
Perhaps a combination of fread and strread will help you?!?!
Lars
Lars Gregersen
www.rndee.dk
l...@rndee.dk