Print variable text with a numerical variable.
3 次查看(过去 30 天)
显示 更早的评论
Hi
I'm working thermodynamics, so there's a bunch of R constants that I can use and they all have different units, the user can choose this constant
I want the user to give me values for PVTn and calculate the one that is missing if so. Then I print it in a message box, but I want the text to change according to the R constant chosen so it can say "The temperature is: x K" or "The temperature is x: °C"
I have 4 states, 3 variables for each state (P1, V1, T1, P2, V2...), so I don't want to do a lot of "if", I was trying to create a new variable and print it in the message box, but it won't let me do that
Thanks in advance for your help
erres={'8.314','0.08314','83.14','0.00008314','0.08206','1.98','1.987','10.73'};
[menu1] = listdlg('PromptString',...
{'Selecciona un valor de R para realizar los cálculos'},...
'SelectionMode','single','ListString',erres,'ListSize',...
[300,100],'name','Constante R');
if menu1==1
R=8.314;UP=({'Pa'});UV='m^3';UT='K';%This is the text that I'm trying to print after the variable
end %I didn't include the other options of the menu, but there are 8 options
%The display looks like this
if P1==0
P1 = (n*R*T1)/V1;
uiwait(msgbox(sprintf('El valor de la presión 1 es: %2.3g %2.3',P1,UP),'P1'));
end %Again, I didn't include the other options, there are 12 in total, that's why I don't want to use if for each R
0 个评论
回答(1 个)
Mara
2020-6-18
What about you make three more vectors, UP, UV and UT...
UP = ["Pa", MPa" , or whatsoever] that have the same length as erres.
Then you use your variable menu 1 to index into these vectors and display it in the messagebox.
You can also index with it into erres:
erres{menu1};
or if you want to convert it into a double that you can calculate with:
str2double(erres{menu1} );
Note: you have to convert them all into strings, which in case of your doubles you can do with num2str();
if P1==0
P1 = (n*R*T1)/V1;
uiwait(msgbox(strcat('El valor de la presión 1 es:',num2str(P1),num2str(UP(menu1))),'P1'));
end
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Startup and Shutdown 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!