matlab - 结构体字段是否可以包含矩阵?

标签 matlab matlab-struct

我正在做一项作业,我必须读取制表符分隔的文本文件,并且我的输出必须是 matlab 结构。

文件的内容如下所示(有点乱,但你明白了)。实际文件包含 500 个基因(从分析物 1 开始的行)和 204 个样本(从 A2 开始的列)

#1.2                                    
500 204                             
Name        Desc        A2  B2  C2  D2  E2  F2  G2  H2
Analyte 1   Analyte 1   978 903 1060    786 736 649 657 733.5
Analyte 2   Analyte 2   995 921 995.5   840 864.5   757 739 852
Analyte 3   Analyte 3   1445.5  1556.5  1579    1147.5  1249    1069.5  1048    1235
Analyte 4   Analyte 4   1550    1371    1449    1127    1196    1337    1167    1359
Analyte 5   Analyte 5   2074    1776    1960    1653    1544    1464    1338    1706
Analyte 6   Analyte 6   2667    2416.5  2601    2257    2258    2144    2173.5  2348
Analyte 7   Analyte 7   3381.5  3013.5  3353    3099.5  2763    2692    2774    2995

我的代码如下:

fid = fopen('gene_expr_500x204.gct', 'r');%Open the given file

% Skip the first line and determine the number or rows and number of samples
dims = textscan(fid, '%d', 2, 'HeaderLines', 1);
ncols = dims{1}(2);

% Now read the variable names
varnames = textscan(fid, '%s', 2 + ncols);
varnames = varnames{1};

% Now create the format spec for your data (2 strings and the rest floats)
spec = ['%s%s', repmat('%f', [1 ncols])];

% Read in all of the data using this custom format specifier. The delimiter     will be a tab
data = textscan(fid, spec, 'Delimiter', '\t');

% Place the data into a struct where the variable names are the fieldnames
ge = data{3:ncols+2}
S = struct('gn', data{1}, 'gd', data{2}, 'sid', {varnames});

关于 ge 的部分是我目前的尝试,但它并没有真正起作用。任何帮助将不胜感激,提前谢谢!

最佳答案

struct 字段可以保存任何数据类型,包括多维数组或矩阵。

您的问题是 data{3:ncols+2} 创建了 comma-separated list 。由于赋值左侧只有一个输出,因此 ge 将仅保存最后列的值。您需要使用 cat 将所有列连接成一个大矩阵。

ge = cat(2, data{3:end});

% Or you can do this implicitly with []
% ge = [data{3:end}];

然后你可以将此值传递给struct构造函数

S = struct('gn', data(1), 'gd', data(2), 'sid', {varnames}, 'ge', ge);

关于matlab - 结构体字段是否可以包含矩阵?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38154008/

相关文章:

matlab - 使用 Matlab 更改文件中的文本行

linux - wfdb Matlab 命令在 Linux 和 OS X 中是否不同?

regex - 如何查询Matlab结构体中匹配特定模式的变量列表?

matlab - 索引/访问 MATLAB 嵌套结构

arrays - 为什么 MATLAB 对结构数组赋值中的字段顺序敏感?

string - 如何格式化字符串以用作 MATLAB 中的结构字段名称?

matlab - 在Matlab中使用mex编译C++时,如何摆脱stdafx.h错误?

r - MATLAB 等效于 R 函数 rank()?

user-interface - 等待 GUI 完成 - MATLAB

matlab - 在 Matlab 中将结构转换为 double 类型