How can I overwrite a part of an m file?

7 visualizaciones (últimos 30 días)
Iman
Iman el 10 de Sept. de 2011
Comentada: Walter Roberson el 31 de Dic. de 2015
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 comentarios
Nimal
Nimal el 30 de Dic. de 2015
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.

Iniciar sesión para comentar.

Respuestas (1)

Walter Roberson
Walter Roberson el 10 de Sept. de 2011
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 comentarios
Iman
Iman el 10 de Sept. de 2011
Dear Walter,
Thank you for your answer.The fact is by typing runpsat(`TestBusSystem_006_mdl.m','data'), the model(.m file which contains PV data)is automatically loaded.It means that for the initialization I have no problem.On the other hand for rest of the loop I don't know how I can overwrite (or lets say substitute) values in the .m file and consequently load and run the procedure with the edited value.
Walter Roberson
Walter Roberson el 31 de Dic. de 2015
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.

Iniciar sesión para comentar.

Categorías

Más información sobre Entering Commands en Help Center y File Exchange.

Community Treasure Hunt

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

Start Hunting!

Translated by