Search code examples
matlabfilelinesnumerical

Reading with Matlab specific lines from a file and converting them to numerical values


I create a code in MATLAB that optimizes an ANSYS study, so i want to check the ANSYS output file and see if the results are acceptable or not.

This code has as imputs parameters that are used by ANSYS to create the model. These parameters change at each iteration, thus at each iteration a different output file is created.

Let's be more specific. Below, there is an example of the output file:

  • line 1 blabla
  • line 2 blabla

  • . . .

  • line 10000 maximum values
  • line 10001 values1 2.31 4.56 5.69 8.64 0.25 9.70
  • .
  • .
  • line 35000 maximum values
  • line 35001 values2 2.25
  • .
  • .
  • line 70000 total values3 2503.4

All i want to do is to see if the first two values in bold are below the limits of the problem (i.e 9.70<15 and 2.25<7). If they are, store the third value in bold in a matrix. If they are not, go to the next iteration.

I'm pretty new to programming and Matlab instructions are a little confusing.

Any ideas would be welcome!

Thanks in advance!

**EDIT:** That's my entire code so far:

    X1=linspace(26,60,3)';
    X2=linspace(104,70,3)';
    R=linspace(3,10,3)';
    vec={X1',X2',R'};
    combs=combvec(vec{:})';

    seqv=zeros(i,1);
    tic

    for i=1:length(combs);
        fid=fopen('C:\Users\vaioss\Desktop\ergasia ymk\test\aa.txt','w+');
        fprintf(fid,'*SET,X1,%7.4f \r\n',combs(i,1));
        fprintf(fid,'*SET,X2,%7.4f \r\n',combs(i,2));
        fprintf(fid,'*SET,R,%7.4f \r\n',combs(i,3));
        fclose(fid);

        fid=fopen('C:\Users\...','r+');
        fclose(fid);

        dos('"C:\Program Files\ANSYS Inc\v150\ANSYS\bin\winx64\ansys150.exe"  -p ...');     

        fid=fopen('C:\Users\...','r');

        for j=1:10152; 
            tline=fgetl(fid); 
        end

        match = textscan(tline, '%s %f %f %f %f %f', '\n')';
        seqv(i) = cell2mat(match(6,1));

        if seqv(i)>67.2887;
            fclose(fid);
            continue
        end


    end
    fclose all;
    toc

Solution

  • If you have an unknown number of lines of varying format before the info you want, probably the easiest way is along these lines

    1) Use fgetl to fetch lines one at a time (obviously if you know you can safely skip the first 3000 lines or whatever, do so)

    2) Use strfind to check if you've hit the values1 line. If so, parse the line and check if the value is within limits.

    Then repeat for values2 and values3 if required, or move onto the next file and repeat. If there's any chance that you have files in the list that don't contain all these strings, then you'll also want some way of handling the case where you hit eof before finding them.