在Matlab中将文字文件名保存为变量

Saving a literal file name as a variable in Matlab

我的目标是加载table中列出的一些文件,提取数据,并将结果保存为第一个文件名的变量。 table中的列表是用户输入的字符,代表即将加载的文件名。我给你举个例子,因为你可能不明白我的意思。当 A、B、C(字符串)列在 table 中时,我的代码将找到它们所在的位置(例如 A.txt)并加载它们的数据。从他们那里收集数据后,结果以 table 的名称保存,如下所示:A(variable)= result_data(:4)。这是我的代码。请让我知道错误的地方。 (注意 table 是使用 uitable 的 nx1 元胞数组。)

function pushbutton1_Callback(hObject, eventdata, handles)

data = get(handles.uitable,'data'); % get strings in table
for i = 1:size(data(:,1))  % count the #strings

fid = fopen([ data(i),'.csv' ]); %load the data and extract what I need
...
fclose(fid);
data(i) = result(row_1:row_2 , 4)  % this is the result_data 
% data(i) is variable string, so I am not sure whether to use the eval function.

结束

无需您 table 进一步调试,这是我的建议。 data 可能是一个元胞数组,因为您是从 uitable 中提取它,如下所示。

data = get(handles.uitable,'data'); % get strings in table

所以这一行应该报错:

fid = fopen([ data(i),'.csv' ]);

改成这样:

fid = fopen([ data{i},'.csv' ]); 

或者这个:

fid = fopen([ char(data(i)),'.csv' ]); 

将结果保存到与字符串匹配的变量名称时,我建议使用具有动态字段名称的结构而不是裸变量...否则您可能不得不使用 eval,这应该避免。

所以这个(这不是你要求的):

data(i) = result(row_1:row_2 , 4)  % this is the result_data 

应该变成:

outData.(data{i}) = result(row_1:row_2 , 4)  % this is the result_data 

如果 data 是一个元胞数组,就像你说的那样包含 {'A','B','C',...} 那么 outData 将是下面的形式并包含每个结果。

outData.A
outData.B
outData.C