matlab常用操作 随手记录
在matlab中,如果需要获取.m文件所在的路径,按照如下方法即可:
p1 = mfilename('fullpath'); % 获取m文件所在的路径
i = findstr(p1, '\');
p1 = p1(1:i(end)-1);
disp(p1);
得到p1为m文件所在的路径
matlab中判断路径是否存在以及创建路径:
例如在路径下创建data文件夹:
data_path = [p1, '\data'];
if ~exist(data_path) % 判断路径是否存在
mkdir(data_path);
end
matlab获取系统时间:参考下表(图表来源:https://blog.csdn.net/xmfthu/article/details/10479327)
Number | String | Example |
0 | 'dd-mmm-yyyy HH:MM:SS' | 01-Mar-2000 15:45:17 |
1 | 'dd-mmm-yyyy' | 01-Mar-2000 |
2 | 'mm/dd/yy' | 03/01/00 |
3 | 'mmm' | Mar |
4 | 'm' | M |
5 | 'mm' | 03 |
6 | 'mm/dd' | 03/01 |
7 | 'dd' | 01 |
8 | 'ddd' | Wed |
9 | 'd' | W |
10 | 'yyyy' | 2000 |
11 | 'yy' | 00 |
12 | 'mmmyy' | Mar00 |
13 | 'HH:MM:SS' | 15:45:17 |
14 | 'HH:MM:SS PM' | 3:45:17PM |
15 | 'HH:MM' | 15:45 |
16 | 'HH:MM | PM'3:45PM |
17 | 'QQ-YY' | Q1-96 |
18 | 'QQ' | Q1 |
19 | 'dd/mm' | 01/03 |
20 | 'dd/mm/yy' | 01/03/00 |
21 | 'mmm.dd,yyyy HH:MM:SS' | Mar.01,2000 15:45:17 |
22 | 'mmm.dd,yyyy' | Mar.01,2000 |
23 | 'mm/dd/yyyy' | 03/01/2000 |
24 | 'dd/mm/yyyy' | 01/03/2000 |
25 | 'yy/mm/dd' | 00/03/01 |
26 | 'yyyy/mm/dd' | 2000/03/01 |
27 | 'QQ-YYYY' | Q1-1996 |
28 | 'mmmyyyy' | Mar2000 |
29 | (ISO 8601)'yyyy-mm-dd' | 2000-03-01 |
30 | (ISO 8601)'yyyymmdd THHMMSS' | 20000301T154517 |
31 | 'yyyy-mm-dd HH:MM:SS' | 2000-03-01 15:45:17 |
time = datestr(now,31); % 获取系统的当前时间
disp(time)
time = strrep(time, '-', '_'); % 字符串替换函数
time = strrep(time, ' ', '_');
time = strrep(time, ':', '_');
disp(time);
结果如下所示:将time做这样的转换可以用time来命名文件夹
将矩阵的值写入excel文件中:
a = [1,2,3,4;
3,4,5,6;
2,4,1,4];
path = [data_path, '\', time, '\', 'name', '.xlsx'];
mkdir([data_path, '\', time])
xlswrite(path, a); % 将矩阵写入excel文件
后面的用到了想起了再补。。。
--------------------------------------------分割线------------------------------------------
保存作图的图片
A = figure(7);
title('发电商报价');
for i=1:genco_num
save_data_price(i,:) = company(i).bid_price;
plot(company(i).bid_price);
hold on;
end
xlabel('竞价轮次');
ylabel('报价');
set(gca, 'XLim', [0 total_loop]);
set(gca, 'XTick', [0:step_axis:total_loop]);
legend('300WM-1', '300MW-2', '600MW-1', '600MW-2', '1000MW');
frame = getframe(A);
image = frame2im(frame);
A_path = [figure_path_full, '\', 'price', image_format];
imwrite(image, A_path);
-------------------------------------------分割线--------------------------------------------