↓

Matlab

在matlab中为绘图中的中英文字符串使用不同的字体

··
废话不多说,直接上代码 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 function out = texWrap(inputStr, options) % 包装字符串,为英文和中文(非英文)分别指定字体,可用于默认的tex解释器,以免出现口口口 % 默认的字体为宋体/Times New Roman % 例如: % xlabel(texWrap("拉力/kN")) % xlabel(texWrap("拉力/kN", cnFontName = "黑体", enFontName = "Tahoma")) % 实践时可以用函数包装目标函数或者wrap函数本身,例如: % xlbl = @(x) xlabel(texWrap(x)); % 或者 % p = @(x) texWrap(x, cnFontName = "黑体", enFontName = "Tahoma") % xlabel(p("拉力/kN")) arguments inputStr {mustBeNonzeroLengthText} options.cnFontName {mustBeTextScalar} = "宋体" options.enFontName {mustBeTextScalar} = "Times New Roman" end if ~isstring(inputStr) inputStr = string(inputStr); end cnFontName = options.cnFontName; enFontName = options.enFontName; function out = texWrapInternal(str) % 使用正则表达式分割字符串 letterRange = "\x00-\x7F\x370-\x3FF"; pattern = "((\s*[^"+ letterRange +"]+)+)|(["+ letterRange +"]+)"; tokens = regexp(str, pattern, "tokens"); segments = string(tokens); delims = ["\fontname{" + cnFontName + "}", "\fontname{" + enFontName + "}"]; if isempty(regexp(segments(1), "(["+ letterRange +"]+)", "tokens")) delims = fliplr(delims); end % 使用两次join拼接最终输出的字符串 pairLen = floor(length(segments)/2); pairs = strings(pairLen, 1); for i = 1:pairLen start = 2*i - 1; pairs(i) = segments(start:start+1).join(delims(1)); end if mod(length(segments), 2) pairs(end+1) = segments(end); end out = delims(2) + (pairs.join(delims(2))); end out = arrayfun(@texWrapInternal, inputStr); end

Matlab平铺所有打开的绘图窗口

··
直接上代码: 这是跟ChatGPT battle了多轮之后的结果(但是还得手动改掉一部分内容) 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 function arrangefigs(yUseRatio, mode) % arrangefigs - 自动排列屏幕上的绘图窗口 % 输入: % yUseRatio - 屏幕Y方向占据的比例,默认为0.95 % mode - 指定为1时,横向排列窗口 % % Get screen size screenSize = get(0, 'ScreenSize'); screenWidth = screenSize(3); screenHeight = screenSize(4); if nargin < 2 mode = 0; end if nargin < 1 yUseRatio = 0.95; else assert(yUseRatio > 0 && yUseRatio <= 1, "yUseRatio需要在(0, 1]范围内") end % Get handles to all open figures figures = flip(findobj('Type', 'figure')); numFigures = length(figures); % Retrieve original sizes of the figures originalSizes = zeros(numFigures, 4); for i = 1:numFigures originalSizes(i, :) = get(figures(i), 'OuterPosition'); end % Calculate the number of columns and rows based on the number of figures averageFigureWidth = mean(originalSizes(:, 3)); averageFigureHeight = mean(originalSizes(:, 4)); if mode == 1 numRows = 1; else numRows = max(1, floor(screenHeight * yUseRatio / averageFigureHeight)); end numColumns = max(1, ceil(numFigures / numRows)); % if (numColumns * averageFigureWidth > screenWidth) % numRows = numRows + 1; % numColumns = max(1, ceil(numFigures / numRows)); % end % Calculate spacing between figures spacingX = max((screenWidth - numColumns * averageFigureWidth) / (numColumns + 1), 0); spacingY = max((screenHeight * yUseRatio - numRows * averageFigureHeight) / (numRows + 1), 0); % Set initial position x = spacingX; % initial x-coordinate y = screenHeight - spacingY - averageFigureHeight; % initial y-coordinate % Iterate through figures and move each figure to the grid position for i = 1:numFigures % Retrieve original size of the current figure originalSize = originalSizes(i, :); % Customize the position of each figure set(figures(i), 'OuterPosition', [x, y, originalSize(3), originalSize(4)]); % Bring the figure to the front figure(figures(i)); % Update position for the next figure x = x + min(originalSize(3) + spacingX, (screenWidth - averageFigureWidth) / (numColumns - 1)); % Check if the next column needs to start in a new row if mod(i, numColumns) == 0 x = spacingX; y = y - min(spacingY + averageFigureHeight, screenHeight / numRows); end end end

在Matlab中绘制Comsol导出的云图数据并导出为图片

··
直接上代码 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 % 设置绘图使用的默认字体(重启matlab后恢复原样),也可单独设置(见后续代码) fntsize = 10; fntname = 'Palatino Linotype'; fnt = {'FontSize',fntsize,'FontName',fntname}; set(groot,'defaultAxesFontName',fntname) set(groot,'defaultAxesFontSize',fntsize) set(groot,'defaultLegendFontName',fntname) set(groot,'defaultLegendFontSize',fntsize) set(groot,'defaultTextFontName',fntname) set(groot,'defaultTextFontSize',fntsize) set(groot,'defaultTextboxShapeFontName',fntname) set(groot,'defaultTextboxShapeFontSize',fntsize) % 读数据,跳过前8行(comsol导出的数据默认有8行注释) % 可以轻松处理百MB的文件 dat = readmatrix('d:\data.txt', 'NumHeaderLines', 8); % comsol默认输出的数据格式为 每行 -- x y 值 x = dat(:, 1); y = dat(:, 2); v = dat(:, 3); % 最终画图时用的x坐标和y坐标 [xq, yq] = meshgrid(-150:.2:150, -35:.2:35); % 将comsol输出的数据插值到均匀网格 vq = griddata(x, y, v, xq, yq); % 图片最大的大小(物理)12cm * 8cm fig = figure('Units', 'centimeters', 'InnerPosition', [0 0 12 8]); tiledlayout('flow', 'TileSpacing', 'none', 'Padding', 'tight'); nexttile; % 移动到屏幕中间 movegui(fig, 'center') % 绘制(pcolor的性能要优于surface,因为是纯二维图) s = pcolor(xq, yq, vq); % 去除默认绘制的网格,并使颜色平滑 s.EdgeColor = 'none'; s.FaceColor = 'interp'; % 颜色映射 colormap(wave()); % y轴范围 ylim([-35 35]); % 使坐标轴的比例为1:1 daspect([1 1 1]); % 轴标签 xlabel('x/mm', fnt{:}); % 使色彩轴对称 cbmaxval = max(abs(vq), [], "all"); caxis(cbmaxval * [-1 1]); % 绘制色卡 cb = colorbar; cb.Label.String = 'A / Pa'; cb.Label.FontName = fntname; cb.Label.FontSize = fntsize; % 注意:matlab默认生成的负号不是标准的负号,有些期刊会介意,可以使用这个修复 cb.TickLabels = strrep(cb.TickLabels,'-',char(8722)); % 调节坐标轴的字体 ax = gca; ax.FontName = fntname; ax.FontSize = fntsize; % 导出图片 exportgraphics(fig, 'file.png', 'Resolution', 600); 常用的comsol颜色映射(如文中的wave)可以到这里下载

在matlab中使用comsol的colormap

··
使用comsol livelink for matlab 执行 1 map = colortable(name); 例如 1 cmap = colortable('WaveClassic'); 个人常用的色卡(wave, waveclassic, wavelight, wavelightclassic)已提取,使用时只需 1 colormap(wave()); 即可。 comsol色卡formatlab.zip