如何在 MATLAB 中将所有 "double" 值转换为 "single"?

How to covert ALL "double" values into "single" in MATLAB?

在我的 matlab 工作区中,我有结构、单元格和变量。需要将所有“双”类型值转换为“单”类型值。

例如来自

4x33 double

我需要赶到

4x33 single

我已经用过这样一段代码:

s = whos;
disp(s)
for i = 1:length(s)
      if strcmp(s(i).class,'double')
          name = s(i).name;
          disp(name);
          assignin('base', name, single(evalin('base', name)));
      end
end

它适用于存储在我的工作区中的变量,但问题是它无法访问存储在工作区中结构中的值。

我怎样才能做到这一点?

将双精度转换为单精度的方法是

b=single(a).

您可以使用 single 作为 structfun:

a.c=5;
b=structfun(@single,a)