How to extract a string from a list and use it with dot notation
Show older comments
I have a list of strings, lets say
stringListUpper = ["ABC","DEF","GHI"].
I would like to extract the strings from the list and use them in dot notation. For example, I would like to assign a value to ABC.a and another value to ABC.b.
I tried something like this:
stringListUpper = ["ABC","DEF","GHI"].
stringListLower = ["abc","def","ghi"];
for i = 1:3
for j = 1:3
stringListUpper(i).stringListLower(j) = 1;
end
end
Is what I'm wanting to do possible and if so, how?
Answers (1)
stringListUpper = ["ABC","DEF","GHI"];
stringListLower = ["abc","def","ghi"];
for i = 1:3
for j = 1:3
AssignTo(stringListUpper(i) + "." + stringListLower(j), i*10+j);
end
end
whos
fieldnames(ABC)
function AssignTo(name, value)
tname = tempname() + ".m";
fid = fopen(tname, 'w');
fprintf(fid, "%s = %.999g;\n", name, value);
fclose(fid);
[filedir, filename, filext] = fileparts(tname);
addpath(filedir);
evalin('caller', filename)
rmpath(filedir)
delete(tname)
end
Categories
Find more on Linear Algebra 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!