How to import specific data from .f06 file in MATLAB?

3 Ansichten (letzte 30 Tage)
Omkar Samel
Omkar Samel am 25 Mär. 2019
Bearbeitet: Omkar Samel am 27 Mär. 2019
I want to write a code which imports specific data from the .f06 file to plot graphs.
  2 Kommentare
Walter Roberson
Walter Roberson am 25 Mär. 2019
Which data do you want to import?
Omkar Samel
Omkar Samel am 25 Mär. 2019
Hi Walter. The first 4 columns of the displacement vector.

Melden Sie sich an, um zu kommentieren.

Akzeptierte Antwort

Walter Roberson
Walter Roberson am 25 Mär. 2019
Bearbeitet: Walter Roberson am 25 Mär. 2019
S = fileread('data.txt');
SS = regexprep(S, '^.*POINT ID.*?\n', '');
datacell = textscan(SS, '%f %s %f %f %*f %*f %*f %*f');
%the line after the last row happens to start with a number, and
%the %s is happy to eat the field after that, but then the %f for the
%third column fails to match. So the first two cells might be one
%entry longer than the third and fourth. Trim away excess
minlen = min(cellfun(@length, datacell));
datacell = cellfun(@(V) V(1:minlen, :), datacell, 'uniform', 0);
%everything is same length now
col1 = datacell{1}; %numeric
col2 = datacell{2}; %cell array of character vectors, probably single characters
col3 = datacell{3}; %numeric
col4 = datacell{4}; %numeric
  8 Kommentare
Walter Roberson
Walter Roberson am 27 Mär. 2019
S = regexp(fileread('data.txt'), '\r?\n', 'split');
Now S will be a cell array of character vectors, one entry per line of input.
Sometimes it can be worth processing in that form, even if just to figure out which entries you want to deal with. But textscan() cannot handle a cell array of character vectors so you would need to strjoin() then back into a block of text if you wanted to use textscan() to do the numeric work.
str2double() also cannot handle multiple numbers per cell array... and besides you have the non-numeric type data in the middle of the line.
str2num() can handle multiple numbers per character vector but str2num() does not permit a cell array of character vectors. str2num() is also pretty dangerous as it is happy to take any character string that appears and execute it as a function call.
sscanf() can handle multiple numbers per character vector, but it too does not permit a cell array of character vectors as input.
So... when you have blocks of inputs on separate lines, your choices reduce to
  1. Use one single character vector as input to textscan(), even if this requires using strjoin() to put cell array of character vectors into one character vector
  2. break up the cell array of character vectors into fields, ending up with a rectangular cell array of character vectors; str2double() can process that. But str2double() cannot process a cell array that contains cell arrays that contain character vectors, so the step about arranging all of the fields into a rectangular array is important
  3. cellfun() textscan() or sscanf() invocations so each call is working on a separate character vector. This has a comparitively high overhead.
  4. careful use of regexp() or regexprep() such as using named tokens, probably followed by some str2double() calls
  5. or fopen() the file, process it line by line, making whatever decisions are appropriate. Sometimes fscanf() in cases where you either know how many lines you will be matching or else expect the pattern to go to end of file. Otherwise fgetl() to fetch one line, examine it to figure out what situation you are in, and potentially sscanf() to extract the values.
  6. Or use other tools. Especially if you are on Mac or Linux, sometimes invoking external tools such as sed can be really worthwhile, even if just to preprocess the file.
Omkar Samel
Omkar Samel am 27 Mär. 2019
Bearbeitet: Omkar Samel am 27 Mär. 2019
Thanks for the explanation. I'll work on this tomorrow and get back to you with my results. Only getting the 3rd and 4th column works for me as well if it makes the coding easy. I have a windowns pc in regards to your last suggestion. Thank you again for the all the help. This makes my work very easy and I also get to learn more about MATLAB.

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (0)

Kategorien

Mehr zu Data Type Conversion finden Sie in Help Center und File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by