↓

Posts

2024

在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

traefik更新之后多hostname的解决方案

··
根据官方文档, All matchers now take a single value (except Header, HeaderRegexp, Query, and QueryRegexp which take two) and should be explicitly combined using logical operators to mimic previous behavior. 需要将原有的 1 - traefik.http.routers.service1.rule=Host(`joyqat.top`, `www.joyqat.top`) 改为 1 - traefik.http.routers.service1.rule=Host(`joyqat.top`) || Host(`www.joyqat.top`)

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

2023

Windows批处理中的路径操作符 (percent tilde %~)

··
需要先知道的是 批处理传入的命令行参数以%1~%9表示,且%0表示批处理文件本身。 常见做法 ​cd /d %~dp0​切换当前目录到批处理文件所在的目录。 ​xxx -i %1 -o "%~dp1%~n1_out%~x1"​使用xxx​命令处理参数1指定的文件,并输出到同一目录下文件名附加_out​,不改变输出文件的后缀名。 解释 Windows批处理中的%~​可以作用于变量名为单个字母/数字的表示路径的字符串变量 功能1:分割路径,输出路径的各个部分。处理过后的字符串默认不包含前后双引号。而%~​后面紧跟的字符则决定了要提取路径字符串的哪一部分。 功能2:获取关于该文件的一些信息。 速查表 假定有变量%F = "d:\abc\efg.txt"​,则有: 命令 解释 输出 %~F 输出不带引号的路径字符串 d:\abc\efg.txt %~dF 输出驱动器符 d: %~pF 输出文件夹路径,不包括驱动器符 \abc\ %~nF 输出文件名,不包括扩展符 efg %~xF 输出扩展名 .txt %~aF 输出文件属性 –a—— %~tF 输出最后修改时间,输出格式与系统区域设置一致 2023/06/28 08:46 %~zF 输出文件大小,以字节计 151040 %~sF (极少使用)输出8.3格式的路径名 其中dpnxs​可以组合使用,例如上面的%~dp0​,常用的还有%~nx​等。 所以不难理解%~dp1%~n1_out%~x1​:%~dp1​为参数1的驱动器符和路径,%~n1​为参数1的文件名部分,_out​为普通的字符串,%~x1​为参数1的文件扩展名。 注意:这个%~​只能用于变量名为单个字母/数字的变量。 注意:这个%~​只能用于变量名为单个字母/数字的变量。 注意:这个%~​只能用于变量名为单个字母/数字的变量。

变形的度量

··
基本定义 # 变形梯度张量$\bm F$ $$ \bm F = \frac{\partial \bm x}{\partial \bm X} \\ F_{i\alpha}=\frac{\partial x_i}{\partial X_\alpha} $$ 左格林-柯西变形张量$\bm C$,右格林-柯西变形张量$\bm B$ $$ \bm C = \bm F^{\rm T}\bm F \\ \bm B = \bm F \bm F^{\rm T} $$ 拉格朗日有限应变张量(格林-拉格朗日应变张量、格林-圣维南应变张量)$\bm E$,欧拉应变张量(欧拉-阿尔曼西有限应变张量)$\bm e$ $$ \bm E=\frac12\left(\nabla_X \bm u + \left(\nabla_X \bm u \right)^{\rm T} +\left(\nabla_X \bm u \right)^{\rm T} \nabla_X \bm u \right)=\frac12(\bm C - \bm I) \\ \bm e=\frac12\left(\nabla_x \bm u + \left(\nabla_x \bm u \right)^{\rm T} +\left(\nabla_x \bm u \right)^{\rm T} \nabla_x \bm u \right)=\frac12(\bm I - \bm B^{-1}) $$ ‍ 应变张量$\bm \varepsilon$ $$ \bm \varepsilon=\frac12\left(\nabla_x \bm u + \left(\nabla_x \bm u \right)^{\rm T}\right) $$

物理声学第17卷目录

··
全书下载 17.1 从超声波谐波测量中确定三阶弹性常数 Determination of Third-Order Elastic Constants from Ultrasonic Harmonic Generation Measurements M. A. Breazeale and Jacob Philip (田纳西大学物理系 Department of Physics, The University of Tennessee, Knoxville, Tennessee) Introduction 2 立方晶体中的纵波谐波生成 Harmonic Generation of Longitudinal Waves in Cubic Crystals 5 介质中传播的平面波运动方程 The Equations of Motion for Plane Elastic Waves Propagating through a Medium 5 立方晶体中沿纯模式方向传播的波 Wave Propagation along Pure Mode Directions in a Cubic Crystal 9 超声非线性参数 The Ultrasonic Nonlineanty Parameter 11 电容接收器用于绝对幅值测量 The Capacitive Receiver as a Tool for Absolute-Amplitude Measurements 12 室温下测量的电容接收器 The Capacitive Receiver for Room-Temperature Measurements 13 电容接收器的校准 Calibration of the Capacitive Receiver 15 可变间距的电容接收器 The Variable-Gap Capacitive Receiver 17 测量技术 The Measurement Technique 18 试件准备 Sample Preparation 18 室温下的测量 Room-Temperature Measurements 18 声速测量 Velocity Measurements 21 低温测量 Cryogenic Measurements 22 一些固体的结论 Results for Some Solids 27 金属(铜) Metals (Copper) 29 半导体锗和硅 Semiconductors Germanium and Silicon 32 离子化合物NaCl和KCl Ionic Solids NaCl and KCl 37 钙钛矿家族 Perovskite Family 40 熔融石英 Fused Silica 46 硅和锗的三阶弹性常数随温度的变化 Temperature Variation of the Third-Order Elastic Constants of Silicon and Germanium 50 方法 The Method 51 结果 Results 53 计算得到的硅和锗的 Gruneisen 参数和热膨胀的温度函数 The Gruneisen Parameter and Thermal Expansion of Silicon and Germanium Calculated as a Function of Temperature 54 Conclusions 56 References 57

Compose V2: docker compose vs docker-compose

··
参考: https://docs.docker.com/compose/migrate/ TL; DR # docker从2020的某个版本之后提供了用go编写的内置的compose命令(称为Compose V2),因此就不需要额外安装使用python编写的docker-compose工具了。在通常的使用条件下,不需要对docker-compose.yml文件进行修改,而只需要将此前使用的CLI从docker-compose xxxx改变为docker compose xxxx即可。

2022

禁用windows文件夹类型自动发现

··
windows会根据文件夹包含的内容,自动将文件夹类型设置为图片、视频等。一般情况下,这个功能无关紧要。但是当一个文件夹内包含的视频文件过多时,打开这个文件夹会在生成缩略图的时间内 (根据文件夹里面的视频数量,可能长达几分钟或更多) 直接卡死资源管理器。在遭遇这个问题时,手动在文件夹属性中设置文件夹类型,不能递归应用到子目录 (即使选中了应用到子目录),只能一个一个手动设置。因此禁用文件夹类型自动发现。 将以下内容保存为.reg文件,双击导入注册表,即可禁用。

禁用Edge推荐设置弹窗

··
如果系统的pdf浏览器不是Edge,或者搜索栏不是必应,那么在使用Edge浏览器时,就有概率定时看到一个恶心人的弹窗,如下图: 解决方案:在Edge的地址栏中输入:edge://flags/#edge-show-feature-recommendations,将高亮的选项改为disabled,并点击页面下方的重启。

COMSOL的批处理功能

··
批量计算 # 将以下内容 (将其中comsol的路径替换为符合自己电脑情况的路径) 保存为.bat文件放在需要计算的文件夹下,双击运行即可计算文件夹中所有.mph文件。待运行完毕后,在output文件夹下会生成相应的计算完毕的模型。

在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)可以到这里下载

资源管理器左边栏快捷方式管理

··
有些软件(百度网盘、OneDrive等)安装后会在资源管理器左侧边栏中创建快捷方式,这些快捷方式可以通过注册表管理,位置为 1 HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Explorer\Desktop\NameSpace

移除windows10/11的xbox相关应用

··
在管理员身份的powershell中运行 1 2 Get-ProvisionedAppxPackage -Online | `Where-Object { $_.PackageName -match "xbox" } |` ForEach-Object { Remove-ProvisionedAppxPackage -Online -AllUsers -PackageName $_.PackageName }

在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