Saving multiple texts to an array

1 次查看(过去 30 天)
Hend Faiad
Hend Faiad 2022-6-12
回答: Jan 2022-6-12
I have writtern this code to ask user to input number of rooms(x) (integer),room area (area(i)) (integer) and room type (string) and based on these selections, Q_env is calculated
This the code
to=input('Enter outside temperature: ');
ti=input('Enter inside temperature: ');
x=input('No of room: ');
for i=1:x
fprintf('ROOM %4.0f:', i)
area(i)=input('Area in meter square = ');
rt=input('Room type: ','s');
end
if rt=="external"
u1=0.6993;
dt=abs(to-ti);
Q_env=u1*area*dt
else if rt=="internal"
u2=1.3858;
dt=abs(to-ti);
Q_env=u2*area*dt
end
end
The following results are displayed when I run the previous code
Enter outside temperature: 40
Enter inside temperature: 20
No of room: 2
ROOM 1:Area in meter square = 20
Room type: internal
ROOM 2:Area in meter square = 30
Room type: external
Q_env =
279.7200
419.5800
419.5800
There are two problems here:
  1. rt only saves the last user input and I need all the room types corresponding to each room area to be saved to an array.
  1. Q_env is diplayed for the given number of rooms and the last result is repeated.
How can I overcome these two issues?

回答(1 个)

Jan
Jan 2022-6-12
You have solved storing the set of areas using: area(i) . To store the set of CHAR vectors in a cell array, use: rt{i} .
If you do not want to display the resulat of a command, append a semicolon:
Q_env = u1 * area * dt;
% ^

类别

Help CenterFile Exchange 中查找有关 Characters and Strings 的更多信息

产品


版本

R2018b

Community Treasure Hunt

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

Start Hunting!

Translated by