2011-03-21 30 views
1

我想要下面的數據中所示的第3行和第4行(= x和y)的值。這是我想將我Matlab代碼:如何將單元格的行中的值轉換爲Matlab中的矢量

fid = fopen('experimental_var.dat'); 

%# read it into one big array, row by row 
fileContents = textscan(fid,'%s','Delimiter','\n'); 
fileContents = fileContents{1}; 
fclose(fid); %# don't forget to close the file again 

x=fileContents(5); 
y=fileContents(6); 

有人幫助提取行3和粘貼下面的數據文件的4給出<x><y>這些值。

<title>(0.707107, 0.707107, 0)</title> 
    <direction>0.707107 0.707107 0 </direction> 
    <x>1.41421 2.82843 4.24264 5.65685 7.07107 8.48528 9.89949 11.3137 12.7279 14.1421 15.5563 16.9706 18.3848 19.799 21.2132 22.6274 24.0416 25.4558 </x> 
    <y>2.08978 3.09925 4.80142 7.05703 9.66079 12.56 15.5897 18.6189 21.5112 24.1628 26.4319 28.2853 29.7518 30.7296 31.3153 31.5626 31.5141 31.2843 </y> 
    <pairs>11781 11564 11349 11136 10925 10716 10509 10304 10101 9900 9701 9504 9309 9116 8925 8736 8549 8364  </pairs> 

回答

4

,而不是讀整個文件中的內容,並分析它們,有可能使用TEXTSCAN只看到你從文件中需要的值:

fid = fopen('experimental_var.dat','r');  %# Open the file 
x = textscan(fid,'%f','HeaderLines',4,...  %# Ignore 4 header lines 
      'Delimiter',' </x>',...   %# Add <x> and </x> as delimiters 
      'MultipleDelimsAsOne',true);  %# Combine delimiters into one 
y = textscan(fid,'%f','Delimiter',' </y>',... %# Add <y> and </y> as delimiters 
      'MultipleDelimsAsOne',true);  %# Combine delimiters into one 
fclose(fid);         %# Close the file 
x = x{1};  %# Remove the cell array encapsulation 
y = y{1};  %# Remove the cell array encapsulation 
相關問題