1

首先感谢您阅读我的问题。
我正在尝试将具有以下格式的文件中的数据导入 Matlab:

#Text  
#Text: Number  
...  
#Text: Number  
Set1:  
1 2  
3 4   
Set2:  
5 6  
7 8   
...  

我想将这些数字转换为以下形式的两个矩阵:
(1 5
3 7)

(2 6
4 8)

我开始只构建这两个矩阵中的第一个。

 Winkel = 15;
 xp = 30;

 M = readtable('Ebene_1.txt')
 M([1:4],:) = [];
 M(:,3) = [];

for i=0:Winkel-1
   A = table2array(M((2+i*31:31+i*31),1))
end

但是这个解决方案只给了我无法转换为法线向量的单元阵列。

I also tried to use the importdata command, but could not find a way to get this to work either. I know there are many other questions similar to mine, but I could not find one where all the data were in a single column. Also, there are many Matlab-commands for importing data into Matlab and I am not sure which would be the best.
First time asking such a question online so feel free to ask me for more details.

4

1 回答 1

0

You can import the data you provided in your sample using readtable, however because of the format of your file you will need to tweak the function a bit.

You can use detectImportOptions to tell the function how to import the data.

%Detect import options for your text file.
opts = detectImportOptions('Ebene_1.txt')

%Specify variable names for your table.
opts.VariableNames = {'Text','Number'};

%Ignore last column of your text file as it does not contain data you are interested in.
opts.ExtraColumnsRule = 'ignore';

%You can confirm that the function has successfully identified that the data is numeric by inspecting the VariableTypes property.
%opts.VariableTypes

%Read your text file with detectImportOptions.
M = readtable('Ebene_1.txt',opts)

Now that you have table M, simply apply basic Matlab operations to obtain the matrices as you specified.

%Find numerical values in Text and Number variables. Ignore NaN values.
A = M.Text(~isnan(M.Text));
B = M.Number(~isnan(M.Number));

%Build matrices.
A = [A(1:2:end)';A(2:2:end)']
B = [B(1:2:end)';B(2:2:end)']

输出:

A =

     1     5
     3     7

B =

     2     6
     4     8
于 2018-07-15T20:38:21.770 回答