필터 지우기
필터 지우기

Turning string into a variable name for a function to use

조회 수: 4 (최근 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 CenterFile Exchange에서 Tables에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by