How can I overwrite a part of an m file?
6 次查看(过去 30 天)
显示 更早的评论
Dear All,
I have a matlab problem.
My main objective is to run a matlab script in a loop and each time change values of another m files which corresponds to my routine.
Here is the main file:
disp('This program tests different locations of DGs in a 6 bus system');
clc
psat;
Busnumber=5;
ii=1 %first bus
while (ii<6)
c=ii;
runpsat(`TestBusSystem_006_mdl.m','data')
runpsat opf
ii=ii+1;
end
In each iteration, PSAT is run and OPF is calculated. As mentioned earlier, In each iteration a change is the corresponding file named as TestBusSystem_006_mdl.m
PV.con = [ ...
1 100 400 0.9 1.05 1.5 -1.5 1.1 0.9 1 1;
3 100 400 0.6 1.05 1.5 -1.5 1.1 0.9 1 1;
5 100 400 0.94 1.05 1.5 -1.5 1.1 0.9 1 1;
];
The (3,4) element shows the output of added generator to bus number 5 (.94 pu)
For the further iterations this value should be substitute by other amounts.
The bottom line is how I can over write a new value in each iteration.
3 个评论
Nimal
2015-12-30
I am also having this problem. If someone could please explain how to tackle the problem, it would be a great help. I have to run at least 500 iterations with pf and cpf and so manual editing is not an option.
回答(1 个)
Walter Roberson
2011-9-10
I suggest that you edit TestBusSystem_006_mdl.m to load() PV and to move the initialization of PV in to your driver file, which would initialize the PV values and save() them in to a .mat file each time through the loop.
2 个评论
Walter Roberson
2015-12-31
fopen() the file to be changed, for reading. fopen() another file for writing. fgets() from the input and fwrite() to the output until you reach the line that needs to be changed; read that line, modify the string, and fwrite() or fprintf() the modified string. Go back to fgets()/fwrite() until you reach the end of file. fclose() both files. rename the second file to the appropriate name to use it. If there is a function stored in the .m file, use "clear" on the name of the function, like
clear TestBusSystem_006_mdl
However, I need to ask: considering that it is a .m file, why do you not just modify the code in it to read the necessary value from a file? Then all you would need to do is modify the data file. For example,
PV.con = [ ...
1 100 400 0.9 1.05 1.5 -1.5 1.1 0.9 1 1;
3 100 400 0.6 1.05 1.5 -1.5 1.1 0.9 1 1;
5 100 400 0.94 1.05 1.5 -1.5 1.1 0.9 1 1;
];
V34 = load('V34.txt', '-ascii'); %new
PV.con(3,4) = V34; %new
Then you would not need to modify the .m file each time: you would just write a new G34.txt file. For example,
val34 = val34 + 0.01;
fid = fopen('V34.txt', 'wt');
fprintf(fid, '%f\n', val34);
fclose(fid);
This is much easier than modifying the .m file each time.
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Entering Commands 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!