Can I name part of a variable after a string?
2 visualizaciones (últimos 30 días)
Mostrar comentarios más antiguos
Rafael Reis
el 20 de Jul. de 2018
Comentada: Rafael Reis
el 21 de Jul. de 2018
Hello! I'm a new user, so sorry if I'm doing a stupid question, but here is my problem: Instead of having this program:
CO2_Tc = 304.12;
H2O_Tc = 647.14;
Name = input('Input name: ','s');
switch Name
case 'CO2'
T = input('Input temperature: ');
Tc = CO2_Tc;
Tr = T/Tc
case 'H2O'
T = input('Input temperature: ');
Tc = H2O_Tc;
Tr = T/Tc
otherwise
disp('Invalid name')
end
Could I have something like:
CO2_Tc = 304.12;
H2O_Tc = 647.14;
Name = input('Input name: ','s');
T = input('Input temperature: ');
Tc = (Name)_Tc; %Here, Name would be replaced by the user's input, like CO2 or H2O
Tr = T/Tc
Is it possible?
0 comentarios
Respuesta aceptada
Walter Roberson
el 20 de Jul. de 2018
It is possible, but firmly not recommended
What I would recommend in your case is to have something like
Tc_vals = struct('CO2', CO2_Tc, 'H2O', H2O_Tc);
and then you could use
Tc = Tc_vals.(Name);
3 comentarios
Walter Roberson
el 21 de Jul. de 2018
You can use cell2struct to create multiple struct entries in one call without having to list them out.
Más respuestas (0)
Ver también
Categorías
Más información sobre Characters and Strings en Help Center y File Exchange.
Productos
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!