Commit 94bfd925 by NanaSwan

fix: change to UTF-8 format

parent 9d32971e
function convert_m_file(input_file, output_file) function convert_m_file(input_file, output_file)
% input_file: 输入.m文件路径(例如 'case14.m') % input_file: 输入.m文件路径(例如 'case14.m')
% output_file:输出.txt文件路径(例如 'case14_mems.txt') % output_file:输出.txt文件路径(例如 'case14_mems.txt')
% 将matpower矩阵格式转换为mems支持的脚本矩阵格式 % 将matpower矩阵格式转换为mems支持的脚本矩阵格式
% 可识别mpc的bus、gen、branch、gencost矩阵(第21行处可手动增删) % 可识别mpc的bus、gen、branch、gencost矩阵(第21行处可手动增删)
% 读取输入文件 % 读取输入文件
run(input_file); run(input_file);
mpc = ans; mpc = ans;
% 打开输出文件 % 打开输出文件
fid = fopen(output_file, 'w'); fid = fopen(output_file, 'w');
if fid == -1 if fid == -1
error('无法打开文件'); error('无法打开文件');
end end
% 写入baseMVA % 写入baseMVA
fprintf(fid, 'baseMVA = %d;\n\n', mpc.baseMVA); fprintf(fid, 'baseMVA = %d;\n\n', mpc.baseMVA);
% 固定读取的矩阵名称并遍历、转换 % 固定读取的矩阵名称并遍历、转换
matrix_names = {'bus', 'gen', 'branch', 'gencost'}; matrix_names = {'bus', 'gen', 'branch', 'gencost'};
for i = 1:length(matrix_names) for i = 1:length(matrix_names)
matrix_name = matrix_names{i}; matrix_name = matrix_names{i};
...@@ -33,11 +33,11 @@ function convert_m_file(input_file, output_file) ...@@ -33,11 +33,11 @@ function convert_m_file(input_file, output_file)
end end
fclose(fid); fclose(fid);
disp(['文件已转换并保存为: ', output_file]); disp(['文件已转换并保存为: ', output_file]);
end end
function formatted_matrix = format_matrix(input_matrix) function formatted_matrix = format_matrix(input_matrix)
% 输入矩阵input_matrix,输出格式化后的formatted_matrix % 输入矩阵input_matrix,输出格式化后的formatted_matrix
[num_rows, ~] = size(input_matrix); [num_rows, ~] = size(input_matrix);
formatted_matrix = []; formatted_matrix = [];
...@@ -45,7 +45,7 @@ function formatted_matrix = format_matrix(input_matrix) ...@@ -45,7 +45,7 @@ function formatted_matrix = format_matrix(input_matrix)
for i = 1:num_rows for i = 1:num_rows
row = input_matrix(i, :); row = input_matrix(i, :);
% 将该行的元素转换为逗号分隔的字符串 % 将该行的元素转换为逗号分隔的字符串
formatted_row = strjoin(arrayfun(@(x) num2str(x), row, 'UniformOutput', false), ','); formatted_row = strjoin(arrayfun(@(x) num2str(x), row, 'UniformOutput', false), ',');
if i ~= num_rows if i ~= num_rows
......
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论