function [data,f] = pch2mat(filename,freq_count);
%%This function converts NASTRAN PCH file to a data structure and frequency
%vector. A filename as a string and the number of frequencies as an integer
%are needed as inputs.

%% Open the file and read the file line by line to form a line character array
%filename='20110728_v1-5-bip_mobility.pch';
%filename='onepoint.pch';
fid = fopen(filename); 
l=1;
while (~feof(fid))
    raw{l}=fgets(fid);
    l=l+1;
end

%%
%_resol=1;
%freq_count=151;

%% Read from array
lines= (freq_count * 4 + 7); %number of lines on each subcase and related point id
C = length(raw) / lines; %number of subcase and related point id

for k = 1 : C; %looping through every case
    scase = char(raw{(k-1) * lines + 3}(12:13))
    pid = char(raw{(k-1) * lines + 7}(18:24))
    pid = genvarname(pid);
    f = zeros(freq_count,1);
    data.(scase).(pid).x=zeros(freq_count,2);
    data.(scase).(pid).y=zeros(freq_count,2);
    data.(scase).(pid).z=zeros(freq_count,2);
    data.(scase).(pid).r1=zeros(freq_count,2);
    data.(scase).(pid).r2=zeros(freq_count,2);
    data.(scase).(pid).r3=zeros(freq_count,2);
    i = (k-1) * lines + 8 ;
    j = 0;
    
    while( i <= (lines * k))
        j=j+1;
        f(j) = str2double(char(raw{i}(5:17)));
        data.(scase).(pid).x(j,1) = str2double(char(raw{i}(25:37)));
        data.(scase).(pid).y(j,1) = str2double(char(raw{i}(43:55)));
        data.(scase).(pid).z(j,1) = str2double(char(raw{i}(61:73)));
        i=i+1;  
        data.(scase).(pid).r1(j,1)=str2double(char(raw{i}(25:37)));
        data.(scase).(pid).r2(j,1)=str2double(char(raw{i}(43:55)));
        data.(scase).(pid).r3(j,1)=str2double(char(raw{i}(61:73)));
        i=i+1;
        data.(scase).(pid).x(j,2)=str2double(char(raw{i}(25:37)));
        data.(scase).(pid).y(j,2)=str2double(char(raw{i}(43:55)));
        data.(scase).(pid).z(j,2)=str2double(char(raw{i}(61:73)));
        i=i+1;
        data.(scase).(pid).r1(j,2)=str2double(char(raw{i}(25:37)));
        data.(scase).(pid).r2(j,2)=str2double(char(raw{i}(43:55)));
        data.(scase).(pid).r3(j,2)=str2double(char(raw{i}(61:73)));
        i=i+1;
    end
end


