2016-07-01 9 views
2

私はタブ区切りのテキストファイルを読み込まなければならない課題に取り組んでいます。私の出力は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約部分は私です現在の試みではありませんが、実際には動作しません。どのような助けも非常に高く評価されます、事前に感謝!

答えて

2

structフィールドは、任意のの多次元配列または行列を含むデータ型を保持できます。

data{3:ncols+2}comma-separated listを作成しています。あなたは割り当ての左側に1つの出力しかないので、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); 
+0

にこの値を渡すことができ、再びSueverをありがとう!それは完璧に働いた。私は今これらのフィールドを使ってプロット/グラフを構築することができますか? – embryo3699

+0

はい、ドット表記を使って任意のフィールドにアクセスできます: 'plot(S.ge(:、1))'など – Suever

関連する問題