フィルターのクリア

Turning string into a variable name for a function to use

2 ビュー (過去 30 日間)
ET
ET 2024 年 1 月 30 日
移動済み: Stephen23 2024 年 1 月 31 日
Hello All,
I am using a loop for converting table to array. The seris of files and variable names are stored in xlsx. While loading the table from file is successful, I have problem coverting the table to array. The filename (string) is also the variable name, but matlab do not see it. Please help! Thanks,
Cinfo = readtable('Z:\subj_studied\Lo_freq\EMUdata\channel\make_721_channel.xlsx');
Cinfo = table2cell(Cinfo);
Contact = Cinfo(:,1); Contact = cell2mat(Contact);
f2load = Cinfo(:,2); f2load = string(f2load);
loaddir = 'Z:\subj_studied\Lo_freq\EMUdata\channel';
savedir = 'Z:\subj_studied\Lo_freq\EMUdata\channel\data';
for ci = 1: length(contact)
load( [loaddir '\' char(f2load(ci))] )
A = table2array(f2load(ci));
save([loaddir '\contact_' char(contact(ci))], 'A')
clear A
end
>> f2load(ci)
ans =
"POL_IM02"
>> A = table2array(f2load(ci));
Error using table2array
Input argument must be a table.
  5 件のコメント
Voss
Voss 2024 年 1 月 31 日
@ET, unrelated to the question, but related to the code in the question:
Note that you define the variable "Contact" (with an upper-case first C) here:
Contact = Cinfo(:,1); Contact = cell2mat(Contact);
but you use "contact" (with a lower-case first c) here:
for ci = 1: length(contact)
% ... ^
save([loaddir '\contact_' char(contact(ci))], 'A')
% ... ^
end
I assume those are supposed to be the same thing.
If you happen to have a variable called "contact" already in the workspace that is equivalent to the variable "Contact" defined in your script, then that would explain why your script runs correctly now (perhaps "contact" came from a run of a previous version of the script that defined "contact" the same exact way "Contact" is defined now, for instance), but if you want your script to run in general (e.g., immediately after restarting MATLAB), then you should make those usages all the same, e.g., either all "contact" or all "Contact".
ET
ET 2024 年 1 月 31 日
Thanks Voss,
it was corrected.
save([savedir '\contact_' num2str(contact(ci))], 'B')

サインインしてコメントする。

採用された回答

Voss
Voss 2024 年 1 月 30 日
I understand that f2load(ci) is a string, and you want to refer to the table variable whose name is that string.
One way to do that is to load into a structure (called S in the code below), so that each loaded variable becomes a field of that structure, and then refer to the particular field of the structure you want using the syntax S.(f2load(ci))
for ci = 1:numel(Contact)
S = load(fullfile(loaddir,f2load(ci)));
A = table2array(S.(f2load(ci)));
% ... then save ...
end
  2 件のコメント
ET
ET 2024 年 1 月 30 日
Thank you so much, Voss, it works.
Voss
Voss 2024 年 1 月 31 日
You're welcome!

サインインしてコメントする。

その他の回答 (1 件)

VBBV
VBBV 2024 年 1 月 30 日
As the error states, input arguments must be table, you need to either convert the f2load into table using table function or pass it simply as an string array without use of function table2array
  2 件のコメント
ET
ET 2024 年 1 月 30 日
The table is loaded from load( [loaddir '\' char(f2load(ci))] ) and f2load(ci) is also the name of the table in workspace in string format. table2array just doesn't recognize it as a variable. Thanks,
VBBV
VBBV 2024 年 1 月 30 日
Cinfo = table2cell(Cinfo); % this line
The above line converts the table to a cell array. So i guess you need to convert the f2load back to table using cell2table and then give it as input to table2aray function

サインインしてコメントする。

カテゴリ

Help Center および File ExchangeTables についてさらに検索

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by