필터 지우기
필터 지우기

change values in a text file, save it and run it with an executable

조회 수: 4 (최근 30일)
Christina
Christina 2011년 8월 31일
Hello all!
I've got a text file that contains all the necessary parameters (80 in total) that I need in order to run it with an executable that I have.
I need to start a sensitivity analysis for one parameter of my text file, one at a time.
For example , in my txt file, I've got:
comments... comments... parameter A = [5 5 5 5] comments...
I want to change the values of parameter A, from 1 to 100, but each time, after every change of the parameter, I want to save the output of the text file, in order to run it with my executable file.
In simpler words, I want to open the txt file, change the parameter value,close the txt file, run it with the .exe file and save the output. And I want to do this 100 times.
Please help!
  댓글 수: 1
Fangjun Jiang
Fangjun Jiang 2011년 8월 31일
You need to provide an example of your text file, especially how to identify and locate the parameter A. Basically you need to provide the text file to your .exe 100 times. Each time, the value of parameter is changed, right?

댓글을 달려면 로그인하십시오.

답변 (1개)

Christina
Christina 2011년 9월 16일
My text file looks like:
comments.......
comments.......
5, 5, 5, 5, ! parameter A
That line (with the parameter A) is located at number 188.
So, the code that I've written in order to locate it is:
%Define the range of the parameter A
%Assume that A can vary between 0.01 and 0.05
A_range = 0.01:0.01:0.05;
%The loop counter
for i=1:length(A_range)
%Extract each value from the range of the A_range
param1 = A_range(i)
%Replicate each of these values 4 times and create the new
%parameter set
A = repmat(param1,1,4)
%Open the jin file and scan through it
fid = fopen('....THE NAME OF MY TEXT FILE.txt);
C = textscan(fid,'%s','delimiter','\n');
% Find the line no 188
line188 = C{1}{188}
and this code is going to give me:
0.05 0.05 0.05 0.05 ! parameter A
The problem is that I need to change these values in the text (which should be formatted as strings), but because I need to loop lots of times for a big range of samples, I need to change these values as numbers in my code.
  댓글 수: 3
Christina
Christina 2011년 9월 16일
Thanks!
however, this is giving me
b = [5]
I need to substitute the values of: 5 5 5 5
with 0.01 0.01 0.01 0.01
0.02 0.02 0.02 0.02
0.03 0.03 0.03 0.03
and so on.
I also tried strrep, but it is not helpful for my case, because I've got thousands of loops.
Fangjun Jiang
Fangjun Jiang 2011년 9월 16일
Assume line188 is correct, textscan() should give the correct value. Then you need to wrap this in your for-loop.
line188='0.05 0.05 0.05 0.05 ! parameter A';
b=textscan(line188,'%f');
b=b{1}
b =
0.0500
0.0500
0.0500
0.0500

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Data Import and Export에 대해 자세히 알아보기

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by