Turning string into a variable name for a function to use
Show older comments
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 Comments
The basic problem is that you are LOADing directly into the workspace. You have already painted yourself into a corner. However, when you LOAD into an output variable then you can easily avoid this ugly situation**.
For example, assuming that each MAT file contains exactly one variable:
C = struct2cell(load(..));
A = table2array(C{1});
"The filename (string) is also the variable name"
Having a series of MAT file whose variable names change like that (e.g. matching the filename) should be avoided. Much more robust data design would use exactly the same variable name/s in every MAT file (then you can simply and robustly LOAD each file and explicitly refer to the fieldnames).
** and it really is a very ugly situation you are getting yourself into:
Stephen23
on 30 Jan 2024
Tip: use FULLFILE to replace the string concatenation and explicit filepath separators.
VBBV
on 30 Jan 2024
can you tell why you have a filename as variable name with string it is built-in function in MATLAB.? when you do
f2load = string(f2load); % filename ... i suppose a function name with string ??
usually, the above command produces string outputs which table2array doesnt recognize.
Voss
on 31 Jan 2024
@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
on 31 Jan 2024
Accepted Answer
More Answers (1)
VBBV
on 30 Jan 2024
0 votes
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
Categories
Find more on LaTeX in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!