在MATLAB中为数据库插入序列化多维数组的最简单方法? [英] easiest way to serialize multi-dimensional array in MATLAB for database insertion?

查看:212
本文介绍了在MATLAB中为数据库插入序列化多维数组的最简单方法?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我似乎找不到用于序列化数据的任何标准函数.我有一个很大的128x51数组,需要将其存储在单个数据库字段中.没有某种序列化,事情就会成问题.

I can't seem to find any standard functions for serializing data. I have a large 128x51 array that I need need to store in a single database field. Without some kind of serializing, things will be problematic.

解决此问题的最佳方法是什么?我使用Matlab的次数不多,所以我对标准程序不熟悉...

Whats the best way to solve this? I don't use matlab quite that much so I'm not familiar with standard procedures...

推荐答案

一种可能性是使用 TYPECAST 函数将数值转换为UINT8字节(仅适用于完整的,非复杂的数值).

One possibility is to use the TYPECAST function to convert numeric values into UINT8 bytes (only works for full, non-complex numeric values).

请注意,在序列化之前必须将矩阵重塑为向量,因此还必须单独存储其大小(甚至使用相同的过程进行序列化):

Note that the matrix has to be reshaped into a vector prior to serialization, thus its size will also have to be separately stored (or even serialized using the same process):

%# sample matrix
M = rand(3,4);

%# convert
b_sz = typecast(size(M),'uint8');   %# serialized matrix size
b = typecast(M(:),'uint8');         %# serialized vector

现在您可以将bb_sz作为字节序列(在[0,255]范围内的整数)存储到数据库中

Now you can store b and b_sz into the database as sequences of bytes (integers in the range [0,255])

>> whos b b_sz
  Name       Size            Bytes  Class    Attributes

  b         96x1                96  uint8              
  b_sz      16x1                16  uint8          

接下来,当您从数据库中检索这些值时,可以使用逆过程将它们转换回为double值,然后将矩阵重塑为原始大小:

Next, when you retrieve those values from DB, you can convert them back to double values using the inverse procedure, and reshape the matrix to its original size:

MM = reshape(typecast(b,'double'), typecast(b_sz,'double'));

%# compare against original matrix
isequal(M,MM)


或者,如果您的数据库不支持数组类型,则可以将字节序列转换为字符串,并将其存储在VARCHAR类型的字段中:


Optionally, if your database does not support array types, you can just convert the byte sequence as string, and store it in a VARCHAR type of field:

%# as string
str = sprintf('%d ',b);

%# recover bytes from string
b = uint8(str2num(str));

这篇关于在MATLAB中为数据库插入序列化多维数组的最简单方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆