0

I was not sure which title fits better in my case so I just left it to be written in the most general way. I have the file with following structure:

Step 1
0.10190103    
0.10145140    
0.10097524    
0.10050153    
0.10003042    
9.95795131E-02
9.91610140E-02
Step 2
9.81189385E-02
9.75561813E-02
9.80424136E-02
0.10000000    
0.10000000    
9.80617628E-02
9.77829769E-02
...
Step N
0.10000000    
0.10000000    
9.93788019E-02
0.11977901    
0.12290157    
0.12588248    
0.12861508

And I need to read it from N arrays, so

V1 = [0.10190103    
0.10145140    
0.10097524    
0.10050153    
0.10003042    
9.95795131E-02
9.91610140E-02]

V2 = [9.81189385E-02
9.75561813E-02
9.80424136E-02
0.10000000    
0.10000000    
9.80617628E-02
9.77829769E-02]

VN = [0.10000000    
0.10000000    
9.93788019E-02
0.11977901    
0.12290157    
0.12588248    
0.12861508]

I have read some examples to read data into arrays, but have not got something related to my case...

Can someone help me please..?

My Try

d = readtable('whole res.txt'); % name of file 
d = table2cell(d);  
d = regexprep(d,' ','');
d = cellfun(@str2double,d,'uni',0);
d(cellfun(@isnan,d)) = [];
d = reshape(cell2mat(d),12,[]); % 12 - is values number

But still does not work. Traceback:

Error using readtable (line 143)
Reading failed at line 2. All lines of a text file must have the same number of delimiters. Line 2 has 4
delimiters, while preceding lines have 1.

SAMPLE FILE

Step 1
0.10000000    
0.10000000    
0.10000000    
0.10000000    
0.10000000    
0.10000001    
0.10000001    
0.10000001    
0.10000000    
0.10000001    
0.10000001    
0.10000001    
Step 2
0.10000000    
0.10000000    
0.10000000    
0.10000000    
0.10000000    
0.10034978    
0.10070127    
0.10070127    
0.10000000    
0.10000001    
0.10000001    
0.10000001 

PrintScreen

enter image description here

2
  • Are there same number of values under every step? Commented Mar 15, 2018 at 13:30
  • Yes, there are the same number of values Commented Mar 15, 2018 at 13:32

1 Answer 1

1

Assuming data has STRICT format, i.e.:
- a line is either a "Step line" or a number
- a "Step line" is necessarily of the format "Step N" where N is an integer
- as many numbers per step can appear

Then here's a solution. Feel free to study it and adapt to your needs.

function Out = readData (In)
  C = strsplit (fileread (In));
  Out = cell(1,1); AtStepcount = false; CurrIndex = 0;

  for I = 1 : length (C)
    if strcmp (C{I}, 'Step'); AtStepcount = true; continue; end
    if AtStepcount
      CurrIndex = str2double(C{I}); Out{CurrIndex} = []; AtStepcount = false; 
      continue
    end
    Out{CurrIndex} = [Out{CurrIndex}; str2double(C{I})];
  end
end

Usage:

V = readData ('myDataFile');

Note: this returns a single cell array, with the step numbers used as indices for each cell element created. This is how you should be doing it anyway; it's generally a bad idea to have variables named "V1", "V2" etc, since you can't iterate using this. With the above solution, your arrays are stored as V{1}, V{2}, etc, and V itself can be used in a for loop.

Sign up to request clarification or add additional context in comments.

6 Comments

Thank you! On rather small files it is working correct, but if file is huge (aroung 1000 steps) program crushes with "Cell contents indices must be greater than 0" pointing to line Out{CurrIndex} = [Out{CurrIndex}; str2num(C{I})];
@Tasos str2num uses eval. You may want to use str2double instead. Double quotes require >= R2017a. The OP is using R2016a. You may want to use single quotes instead.
Please, no offense. Just my computational work is stuck here because I can't go further without visualization ..
Thanks @SardarUsama, I did not know this. I also didn't even spot I was using double quotes ... dangers of getting used to octave too much, heheh.
@John the error tells me that your file probably deviates from the STRICT structure I pointed out. Perhaps there's some sort of inconsistency in the data file that fails? Feel free to share the offending datafile online somewhere for us to test.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.