2

我有一个这样的文件(param.txt):

JSS 2
ADEV 1
VERS 770
JSD 1

我想将此文件中的数据放入工作场所中带有变量的结构中。

假设我称它为“P”,那么 P 是以下结构:

Field    Value
_____  |_______
JSS    |2  
ADEV   |1  
VERS   |770  
JSD    |1  

然后:

>>> P.JSS
ans = 
2

可能吗?

谢谢!

4

1 回答 1

4

是的,您可以使用textscan抓取所有部件,然后使用struct构造函数创建您的单元格。

fid = fopen('filename.txt', 'r');

% Parse out the fieldnames and numbers
data = textscan(fid, '%s %d');

% Put the strings in the first row and the numbers in the second
alldata = [data{1}, num2cell(data{2})].';

% Pass fieldnames and values to struct()
P = struct(alldata{:});

fclose(fid);
于 2016-07-15T17:25:20.457 回答