How to extract a string from a list and use it with dot notation

5 visualizaciones (últimos 30 días)
Michael
Michael el 11 de Feb. de 2025
Respondida: Walter Roberson el 11 de Feb. de 2025
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?

Respuestas (1)

Walter Roberson
Walter Roberson el 11 de Feb. de 2025
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
Name Size Bytes Class Attributes ABC 1x1 433 struct DEF 1x1 433 struct GHI 1x1 433 struct i 1x1 8 double j 1x1 8 double stringListLower 1x3 274 string stringListUpper 1x3 274 string
fieldnames(ABC)
ans = 3x1 cell array
{'abc'} {'def'} {'ghi'}
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

Categorías

Más información sobre Startup and Shutdown en Help Center y File Exchange.

Etiquetas

Productos


Versión

R2022a

Community Treasure Hunt

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

Start Hunting!

Translated by