0

i am having a status file which is of following format:

'bla bla

1     1   1     9     2    11  0.0100        0.0100     0.01000   
1     2   1     2     1     3  0.503         0.503      0.1709    
1     3   1     2     1     3  0.759         0.759      0.2563    
1     4   1     3     2     5  1.00          1.00       0.2411    
2     1   1     0     1     1  2.00          1.00       1.000     
3     1   1    10     1    11  2.20          0.200      0.2000    
3     2   1    13     2    15  2.40          0.400      0.200
4     1   1    10     1    11  2.20          0.200      0.2000    
4     2   1    10     1    11  2.20          0.200      0.2000    
4     3   1    10     1    11  2.20          0.200      0.2000    

blah blah'

Now how can I know how many number of times 1,2,3 etc appear in the first column?

The answer for the above .sta file would be [4 1 2] (i.e 4 is the number of times 1 appears in the first column).

  • this has been asked many times here on SO.. Once you load the data, you can use any of the following ways to do the counting: http://stackoverflow.com/a/2885175/97160 . Here is another one posted just yesterday: http://stackoverflow.com/a/24601403/97160 – Amro Jul 08 '14 at 05:56
  • for the format you've shown above, you can load the data simply as: `load file.sta -ascii` – Amro Jul 08 '14 at 06:04
  • Error using load Number of columns on line 2 of ASCII file C:\Intern\Slip\e.sta must be the same as previous lines. – user3734298 Jul 08 '14 at 06:16
  • you didnt have the 'bla bla' parts before, so I assumed the file was all space-delimited numbers. You should explain exactly what that part you omitted is, and explain the file structure more clearly.. Anyway I think that's a separate question (how to read/parse such a file) from the current one here (how to count occurrences). But please do your research before creating a new question, there are already hundreds of questions here on Stack Overflow about loading data into MATLAB: http://stackoverflow.com/questions/tagged/matlab+file-io – Amro Jul 08 '14 at 06:24
  • This is the exact format of file. SUMMARY OF JOB INFORMATION: STEP INC ATT SEVERE EQUIL TOTAL TOTAL STEP INC OF DOF IF DISCON ITERS ITERS TIME/ TIME/LPF TIME/LPF MONITOR RIKS ITERS FREQ 1 1 1 17 0 17 0.0100 0.0100 0.01000 3 1 1 9 2 11 2.10 0.100 0.1000 3 2 1 11 2 13 2.20 0.200 0.1000 3 3 1 23 5 28 2.30 0.300 0.1000 THE ANALYSIS HAS COMPLETED SUCCESSFULLY – user3734298 Jul 08 '14 at 06:30

1 Answers1

0

Easy peasy, you can just look at them directly. For example if you read in your .sta file into a variable named data:

sum(data(:, 1) == 1)

1. data(:, 1) look at all of the first column
2. (1.) == 1  create a Boolean list of whether they equal to one
3. sum(2.)    sum up all the trues to find the total number of ones. 

This will give you the number of 1s in the first column.

In case you are interested in performing operations on the rest of the matrix where there are only 1s, 2s etc you can do this:

data_only_ones = data(data(:, 1) == 1, 2:end);

This will give you the remainder of the matrix for where the first columns are ones. ie. for your example:

1   1     9     2    11  0.0100        0.0100     0.01000   
2   1     2     1     3  0.503         0.503      0.1709    
3   1     2     1     3  0.759         0.759      0.2563    
4   1     3     2     5  1.00          1.00       0.2411

The thing to remember is that matlabs array indexing is magic and you can basically do anything.

Fantastic Mr Fox
  • 32,495
  • 27
  • 95
  • 175
  • 1
    @user3734298 If you are new then you would be much better off figuring out how to do it yourself. `Can you put the final code here` well no ... I have other things to do as well! – Fantastic Mr Fox Jul 08 '14 at 05:57
  • [fileName,pathName] = uigetfile('*.sta','Pick a file'); data = textread(fullfile(pathName,fileName),'%s','delimiter','\n'); data_only_ones = data(data(:, 1) == 1, 2:end) this is returning a error:'Undefined function 'eq' for input arguments of type 'cell'.' – user3734298 Jul 08 '14 at 06:00