Discover MakerZone

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn more

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today

Thread Subject:
How to optimize this code?

Subject: How to optimize this code?

From: Francesco Perrone

Date: 23 Aug, 2012 07:20:08

Message: 1 of 5

Hi everybody,

I have a "parent" text file called reference, which is:

VERSION 3.82
CALCULATION 3
OPTIONS 0
PROJNAME
DATE
ENGINEER
NOTES ""
PASSWORD
MSTART CONSTANTS
RHO 1.225
RHOW 1027
VISCOS .0000182
GRAVITY 9.81
MEND

MSTART WINDND
SPMODEL 7
NLAT 31
NVER 45
LATDIM 150
VERDIM 220
LONGLS 340.2
LATLS 0
VERTLS 0
XLV 113.4
YLV 0
ZLV 0
XLW 27.72
YLW 0
ZLW 0
LAMBDA1 0
CohScale 12
COHDEC 3
SCALE 33.6
GAMMA 3.9
YDIML 0
N2 32
YDIMS 0
K1MIN 3
LENGTH 1830
STEP .2233888
UBAR 3
SEED 12
OUTFILE None
DIAM 0
HUBHT 0
TURBHTTYPE 0
TURBBOTTOM 0
GUSTAVT 0
GUSTSPEED 0
TOLERANCE 0
DLONGMIN 0
DLONGMAX 0
Z0MIN 0
Z0MAX 0
MAXITER 14
MAXSEED 100
NFILES 1
UseWindShear 0
WVMODEL 0
MATCHFILE ''
SPACING 0
SAMPLEFREQ 0
MEANSPEED 0
ILAT 0
IVERT 0
GUSTMETHOD 0
DLONG 0
ILAT 0
IVERT 0
LONGGUST 0
LATGUST 0
VERTGUST 0
iLONGGUST 0
iLATGUST 0
iVERTGUST 0
PEAKINESS 0
MAXFRAN 0
MEND

0CONSTANTS
0WINDND

The idea is to generate a set similar files, only changing 4 determined lines of the above file (LENGTH,STEP,UBAR,SEED).

Currently I am using this code:

clc
clear all
close all


V = [4 6 8 10 12 14 16 18 20 22 24 26 28 29];


for j = 1:6
f1 = ['D:\01_department\DLC1-2']
if exist(f1,'dir') ~= 7
    mkdir(f1);
end
fname = [f1 '\' 's0' num2str(j)];
if exist(fname,'dir') ~= 7
    mkdir(fname);
end
 for i = 1:length(V),
filename = [fname '\' num2str(V(i)) '.$PJ'];
 fid = fopen(filename,'w');
 fprintf(fid, 'VERSION 3.82\r\n')
 fprintf(fid, 'CALCULATION 3\r\n')
 fprintf(fid, 'OPTIONS 0\r\n')
 fprintf(fid, 'PROJECTNAME\t\r\n')
 fprintf(fid, 'DATE\t\r\n')
 fprintf(fid, 'ENGINEER\t\r\n')
 fprintf(fid, 'NOTES\t""\r\n')
 fprintf(fid, 'PASSWORD\t\r\n')
 fprintf(fid, 'MSTART CONSTANTS\r\n')
 fprintf(fid, 'RHO 1.225\r\n')
 fprintf(fid, 'RHOW 1027\r\n')
 fprintf(fid, 'VISCOS 0.0000182\r\n')
 fprintf(fid, 'GRAVITY 9.81\r\n')
 fprintf(fid, 'MEND\r\n')
 fprintf(fid, '\r\n')
 fprintf(fid, 'MSTART WINDND\r\n')
 fprintf(fid, 'SPMODEL 7\r\n')
 fprintf(fid, 'NLAT 31\r\n')
 fprintf(fid, 'NVER 45\r\n')
 fprintf(fid, 'LATDIM 150\r\n')
 fprintf(fid, 'VERDIM 220\r\n')
 fprintf(fid, 'LONGLS 340.2\r\n')
 fprintf(fid, 'LATLS 0\r\n')
 fprintf(fid, 'VERTLS 0\r\n')
 fprintf(fid, 'XLV 113.4\r\n')
 fprintf(fid, 'YLV 0\r\n')
 fprintf(fid, 'ZLV 0\r\n')
 fprintf(fid, 'XLW 27.72\r\n')
 fprintf(fid, 'YLW 0\r\n')
 fprintf(fid, 'ZLW 0\r\n')
 fprintf(fid, 'LAMBDA1 0\r\n')
 fprintf(fid, 'CohScale 12\r\n')
 fprintf(fid, 'COHDEC 3\r\n')
 fprintf(fid, 'SCALE 33.6\r\n')
 fprintf(fid, 'GAMMA 3.9\r\n')
 fprintf(fid, 'YDIML 0\r\n')
 fprintf(fid, 'N2 32\r\n')
 fprintf(fid, 'YDIMS 0\r\n')
 fprintf(fid, 'K1MIN 3\r\n')
 fprintf(fid, 'LENGTH %g\r\n', V(i)*610)
 fprintf(fid, 'STEP %g\r\n', V(i)/13.4295)
 fprintf(fid, 'UBAR %g\r\n', V(i))
 fprintf(fid, 'SEED %i\r\n', randi(999,1))
 fprintf(fid, 'OUTFILE None\r\n')
 fprintf(fid, 'DIAM 0\r\n')
 fprintf(fid, 'HUBHT 0\r\n')
 fprintf(fid, 'TURBHTTYPE 0\r\n')
 fprintf(fid, 'TURBBOTTOM 0\r\n')
 fprintf(fid, 'GUSTAVT 0\r\n')
 fprintf(fid, 'GUSTSPEED 0\r\n')
 fprintf(fid, 'TOLERANCE 0\r\n')
 fprintf(fid, 'DLONGMIN 0\r\n')
 fprintf(fid, 'DLONGMAX 0\r\n')
 fprintf(fid, 'Z0MIN 0\r\n')
 fprintf(fid, 'Z0MAX 0\r\n')
 fprintf(fid, 'MAXITER 14\r\n')
 fprintf(fid, 'MAXSEED 100\r\n')
 fprintf(fid, 'NFILES 1\r\n')
 fprintf(fid, 'UseWindShear 0\r\n')
 fprintf(fid, 'WVMODEL 0\r\n')
 fprintf(fid, 'MATCHFILE %c\r\n', '')
 fprintf(fid, 'SPACING 0\r\n')
 fprintf(fid, 'SAMPLEFREQ 0\r\n')
 fprintf(fid, 'MEANSPEED 0\r\n')
 fprintf(fid, 'ILAT 0\r\n')
 fprintf(fid, 'IVERT 0\r\n')
 fprintf(fid, 'GUSTMETHOD 0\r\n')
 fprintf(fid, 'DLONG 0\r\n')
 fprintf(fid, 'ILAT 0\r\n')
 fprintf(fid, 'IVERT 0\r\n')
 fprintf(fid, 'LONGGUST 0\r\n')
 fprintf(fid, 'LATGUST 0\r\n')
 fprintf(fid, 'VERTGUST 0\r\n')
 fprintf(fid, 'iLONGGUST 0\r\n')
 fprintf(fid, 'iLATGUST 0\r\n')
 fprintf(fid, 'iVERTGUST 0\r\n')
 fprintf(fid, 'PEAKINESS 0\r\n')
 fprintf(fid, 'MAXFRAN 0\r\n')
 fprintf(fid, ' MEND\r\n')
 fprintf(fid, '\r\n')
 fprintf(fid, '0CONSTANTS\r\n')
 fprintf(fid, '0WINDND\r\n')
 fclose(fid)
 end
end
 
But this way, the code spends most of the time in writing the output. I would like more doing the same only reading the reference file (with fopen() or other commands) and the changing(printing) only
fprintf(fid, 'LENGTH %g\r\n', V(i)*610)
fprintf(fid, 'STEP %g\r\n', V(i)/13.4295)
fprintf(fid, 'UBAR %g\r\n', V(i))
fprintf(fid, 'SEED %i\r\n', randi(999,1))
within the new files.

Does anybody have an idea how to achieve this result? If possible I also would like to vectorize instead of using for loops.

I thank you all in advance.

Kindest regards,
Francesco


 
 

Subject: How to optimize this code?

From: Justin Ashmall

Date: 23 Aug, 2012 13:44:08

Message: 2 of 5

"Francesco Perrone" <francesco86perrone@yahoo.it> wrote in message <k14lj8$n0k$1@newscl01ah.mathworks.com>...
> Hi everybody,
>
> But this way, the code spends most of the time in writing the output. I would like more doing the same only reading the reference file (with fopen() or other commands) and the changing(printing) only
> fprintf(fid, 'LENGTH %g\r\n', V(i)*610)
> fprintf(fid, 'STEP %g\r\n', V(i)/13.4295)
> fprintf(fid, 'UBAR %g\r\n', V(i))
> fprintf(fid, 'SEED %i\r\n', randi(999,1))
> within the new files.
>

Wow. I don't think we needed the full 200 lines of file and code to get the idea there, but still...

I would read the whole file into a string with fileread() and then use regexprep() to replace the bits I wanted to change, and then write out the modified string. However, this means you'll have to get your head around regular expressions which is a noble, but time consuming, task. As s start, try something like this (untested):

new_length_text = sprintf('LENGTH %g\r\n', V(i)*610);
txt = regexprep(txt, 'LENGTH\s*[\d\.]+', new_length_txt);

Subject: How to optimize this code?

From: Francesco Perrone

Date: 23 Aug, 2012 19:48:07

Message: 3 of 5

"Justin Ashmall" wrote in message <k15c38$9fg$1@newscl01ah.mathworks.com>...
> "Francesco Perrone" <francesco86perrone@yahoo.it> wrote in message <k14lj8$n0k$1@newscl01ah.mathworks.com>...
> > Hi everybody,

> I would read the whole file into a string with fileread() and then use regexprep() to replace the bits I wanted to change, and then write out the modified string. However, this means you'll have to get your head around regular expressions which is a noble, but time consuming, task. As s start, try something like this (untested):
>
> new_length_text = sprintf('LENGTH %g\r\n', V(i)*610);
> txt = regexprep(txt, 'LENGTH\s*[\d\.]+', new_length_txt);

Would you mind to provide me with a piece of code?

It's the first time a deal with text reading/writing and manipulating, and it's not that straightforward at a first sight. For reading the parent file I would do like this:

fopen('parent.txt', 'rt')
f = fopen('inp.txt','rt');
C = textscan(f, '%s', 'Delimiter', '\r\n');
C = C{1};
fclose(f);

but then I don't have a real clue how to use regexprep.

If you could provide me with a sample code I would appreciate.

Regards.

Subject: How to optimize this code?

From: Francesco Perrone

Date: 24 Aug, 2012 06:58:07

Message: 4 of 5

At the moment I am using this solution:

f = fopen(file1,'rt'); % Read input file 1
C = textscan(f, '%s', 'Delimiter', '\r\n');
C = C{1};
fclose(f);

f1 = fopen(file2,'rt' ); % Read input file 2
D = textscan(f1, '%s', 'Delimiter', '\r\n');
D = D{1};
fclose(f1);

f2 = fopen(fullfile(pwd,'reference.prj'),'rt' ); % Read reference file
REF = textscan(f2, '%s', 'Delimiter', '\r\n');
REF = REF{1};
fclose(f2);

REF1 = REF;

num_dlc = numel(D)/3;
DLC = reshape(D,3,num_dlc);

for i=1:num_dlc
    DLC{2,i}=str2num(DLC{2,i});
    DLC{3,i}=str2num(DLC{3,i});
end

for l = 1:num_dlc
    for j = 1:DLC{3,l}
        f1 = [C{1,1} '\' DLC{1,l}];
        if exist(f1,'dir') ~= 7
            mkdir(f1);
        end
        fname = [f1 '\' 's0' num2str(j)];
        if exist(fname,'dir') ~= 7
            mkdir(fname);
        end
        for i = 1:length(DLC{2,l}),
            filename = [fname '\' num2str(DLC{2,l}(i)) '.$PJ'];
            REF1{strncmp('LENGTH', REF, length('LENGTH'))} = sprintf('LENGTH %g', DLC{2,l}(i)*610);
            REF1{strncmp('STEP', REF, length('STEP'))} = sprintf('STEP %g', DLC{2,l}(i)/13.4295);
            REF1{strncmp('UBAR', REF, length('UBAR'))} = sprintf('UBAR %g', DLC{2,l}(i));
            REF1{strncmp('SEED', REF, length('SEED'))} = sprintf('SEED %i', randi(999,1));
            
            fid = fopen(filename,'w');
            txt = sprintf([repmat('%s\r\n',1,size(REF1,1)),'\r\n'],REF1{:});
            fprintf(fid, '%s', txt);
            fclose(fid);
      end
    end
end

toc

and it works fine I guess. Any other suggestion??

Thanks in advance.

Subject: How to optimize this code?

From: Bruno Luong

Date: 24 Aug, 2012 07:21:07

Message: 5 of 5

"Francesco Perrone" <francesco86perrone@yahoo.it> wrote in message <k178lv$cmt$1@newscl01ah.mathworks.com>...
>
> and it works fine I guess. Any other suggestion??
>

No, but your code looks fine to me.

Bruno

Tags for this Thread

What are tags?

A tag is like a keyword or category label associated with each thread. Tags make it easier for you to find threads of interest.

Anyone can tag a thread. Tags are public and visible to everyone.

Contact us