Matlab:是否可以从 ascii 文件导入数据并将它们放入结构中?

Matlab: is it possible to import data from ascii file and put them in a struct?

我有一个这样的文件 (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

可能吗?

谢谢!

是的,您可以使用 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);