readout numeric data from cell with different type of content

3 views (last 30 days)
Hi,
Iam serching for a solution on how to readout my numeric data out of a cell array that also contains some strings.
Example:
'9RevD.01\State=1-A\HF=-308.9791228,-308.9793372,-308.9799726,-308.9809'
'996,-308.9823694,-308.9840141,-308.9858544,-308.9878131,-308.9897844,-'
These are the first two rows of my cell and I want to read out only the numbers after the HF=. The numbers are seperated by commas. But since they also get trauncated by the end of the lines, I want to try to join all the rows of my cell into one row (I thought I could do it with strjoin but its not working; it looks like it only works to join everything in one cell from different columns)
Then I want to readout only the Numbers of these lines and sort them in a x*1 matrix.
Like:
-308.9791228
-308.9793372
and so on.
I can not find a solution to do so. That is why I seek help here!
Additional Information:
Iam working on a script to readout some data from Gaussian Output Logfiles. The biggest problem ist that these logfiles keep changing their internal structure so that Iam not able to use textscan etc. properly since the formatSpec keeps changing.
For example: First there is only text, then there are some input values in a tab delimited list with 3 columns, then there is text again, afterwards energy values with a tab delimited list of 7 columns (of which 2 are text) and so and so on.
So the best way so far I could handle the logfile to work with it was to import the logfile into a cell-array where every cell contains one line of the logfile. Than I kept searching for the parts I really needed and cropped my cell to only this part of data. But then I end up with my example! My "final" array contains now mostly comma seperated energy values (that I want to list) but also some unnessesary text.
The file itself is quite large and I read that most people advise to import everything first in this case and structure the parts you need afterwards. But Iam quite sure there are much better ways to import and structure than to import every line of the logfile into 1 cell of a cell array. So if someone has better alternatives Iam more than happy to hear about!

Answers (1)

Jan
Jan on 16 Dec 2014
Edited: Jan on 16 Dec 2014
C = {'9RevD.01\State=1-A\HF=-308.9791228,-308.9793372,-308.9799726,-308.9809', ...
'996,-308.9823694,-308.9840141,-308.9858544,-308.9878131,-308.9897844,-'};
S = [C{:}];
match = strfind(S, 'HF=') + 3;
value = zeros(numel(match), 1);
len = length(S);
for k = 1:numel(match)
value(k) = sscanf(S(match(k):len), '%g');
end

Categories

Find more on Characters and Strings in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!