注意: 这个问题涉及2011年使用旧的MATLAB版本(R2009a)观察到的问题.根据2016年7月以下的更新,MATLAB中的问题/错误似乎不再存在(使用R2016a进行测试;向下滚动到问题末尾以查看更新).
我正在使用MATLAB R2009b,我需要编写一个更大的脚本,将更大的.zip文件集的内容转换为v7.3 mat文件(带有底层的HDF5-datamodel).读书还可以.问题在于储蓄.实际上没有问题.使用save命令可以很好地保存我的文件.
从某种意义上说,我的问题更多:为什么我在MATLAB中观察到以下令人惊讶的(对我来说)行为?
让我们来看看我的问题.在当前的测试场景中,我将生成一个输出:A -v7.3 mat-file.此.mat文件将包含40 个块作为单个变量.每个变量将从1到40命名为"block_NNN",并包含一个带字段frame和blockNo的结构.场帧包含480x240x65的uint8 imagedata序列(这里只是使用randi生成的随机数据).字段块不包含块编号.
备注:在真实的脚本中(我还没有完成)我将完成上述总共370次,转换总共108GB的原始数据.这就是为什么我关注以下内容.
无论如何,首先我定义一些通用变量:
% some sizes for dummy data and loops: num_blockCount = 40; num_blockLength = 65; num_frameHeight = 480; num_frameWidth = 240;
然后,我生成一些形状和大小与实际原始数据相同的虚拟代码:
% generate empty struct:
stu_data2disk = struct();
% loop over blocks:
for num_k = 1:num_blockCount
% generate block-name:
temp_str_blockName = sprintf('block_%03u', num_k);
% generate temp struct for current block:
temp_stu_value … 有时候,在C中,你这样做:
typedef struct foo {
unsigned int some_data;
} foo; /* btw, foo_t is discouraged */
Run Code Online (Sandbox Code Playgroud)
要在OO-sort-way中使用这种新类型,你可能有这样的alloc/free对:
foo *foo_alloc(/* various "constructor" params */);
void foo_free(foo *bar);
Run Code Online (Sandbox Code Playgroud)
或者,或者init/clear对(可能返回错误代码):
int foo_init(foo *bar, /* and various "constructor" params */);
int foo_clear(foo *bar);
Run Code Online (Sandbox Code Playgroud)
我已经看到使用了以下习语,特别是在MPFR库中:
struct foo {
unsigned int some_data;
};
typedef struct foo foo[1]; /* <- notice, 1-element array */
typedef struct foo *foo_ptr; /* let's create a ptr-type */
Run Code Online (Sandbox Code Playgroud)
alloc/free和init/clear对现在读取:
foo_ptr foo_alloc(/* various "constructor" params */);
void foo_free(foo_ptr bar);
int foo_init(foo_ptr bar, …Run Code Online (Sandbox Code Playgroud) 在MATLAB(r2009b)中,我有一个包含值2147484101的uint32变量.
这个数字(4字节)是在抓取过程中从数字机器视觉相机中提取的.根据我的理解,它具有相机快门速度的单精度形式(应接近1/260s = 3.8ms).
如何使用MATLAB中提供的内容将此32位数转换为IEEE单精度浮点表示?
对于变量n中提到的值,我尝试使用nn = dec2hex(n,16)和hex2num(nn)的组合.但似乎hex2num期望十六进制编码是双精度的而不是单一的,因为它在这里.至少我用这种方法得到了奇怪的数字.
有任何想法吗?
编辑:尝试@Matt的答案如下:
typecast(uint32(2147484101),'single') %# without swapbytes
typecast(swapbytes(uint32(2147484101)),'single') %# with swapbytes
Run Code Online (Sandbox Code Playgroud)
这使:
ans =
-6.3478820e-043
ans =
-2.0640313e+003
Run Code Online (Sandbox Code Playgroud)
我在http://www.h-schmidt.net/FloatApplet/IEEE754.html上尝试了IEEE 754转换器(JAVA applet).
使用:
format hex
typecast(uint32(2147484101),'uint8') %# without swapbytes
typecast(swapbytes(uint32(2147484101)),'uint8') %# with swapbytes
Run Code Online (Sandbox Code Playgroud)
给
ans =
c5 01 00 80
ans =
80 00 01 c5
Run Code Online (Sandbox Code Playgroud)
将这些字节输入applet(十六进制)给出了与MATLAB相同的数字.
开始使用matlab指南,遇到了绊脚石.尽可能简单地说明我的问题就像玩具枪一样简单.gui(名为asas)有一个按钮和一个轴.按钮的回调读取
axesHandle= findobj(gcf,'Tag','axes1');
x=rand(randi(10+20,1),4);
plot(axesHandle, x)
Run Code Online (Sandbox Code Playgroud)
我没有写其他代码(指南写了).
我第一次按下按钮,一切都很好:情节完成了.第二次,我从控制台得到一个错误:
Error using plot
Vectors must be the same lengths.
Error in asas>pushbutton1_Callback (line 83)
plot(axesHandle, x)
Error in gui_mainfcn (line 96)
feval(varargin{:});
etc...
Run Code Online (Sandbox Code Playgroud)
我想绘制新数据x,替换旧数据.
看起来matlab没有替换数据来绘图,但不知何故试图追加到情节?
我搜索过,但没有发现任何适用的东西.