How to fix error using horzcat when trying to display?

2 views (last 30 days)
I'm trying to learn matlab. How am I supposed to display to values in the same line without getting horzcat error? What am I doing wrong? Any help is appreciated.
a = [-3.821; 0.135; 5.995; -5.557; -4.041; -3.094; -3.244; -3.074; -1.241; -4.216; -2.834; -0.424; 5.337; -0.088; 2.985; 4.136; 1.939; 6.031; -2.607; 3.340]
b = [-1.529; -2.882; 1.287; 3.822; 0.422; -4.387; 3.107; -3.513; 4.760; -5.205; 2.265; -4.442; 6.247; -2.788; -2.688; -1.761; 1.987; 4.484; 0.052; -2.763]
c = (a.^2 + b.^2).^(1/2)
d = atand(a./b)
e = d;
d(d<0)=360+d(d<0)
a =
-3.8210
0.1350
5.9950
-5.5570
-4.0410
-3.0940
-3.2440
-3.0740
-1.2410
-4.2160
-2.8340
-0.4240
5.3370
-0.0880
2.9850
4.1360
1.9390
6.0310
-2.6070
3.3400
b =
-1.5290
-2.8820
1.2870
3.8220
0.4220
-4.3870
3.1070
-3.5130
4.7600
-5.2050
2.2650
-4.4420
6.2470
-2.7880
-2.6880
-1.7610
1.9870
4.4840
0.0520
-2.7630
c =
4.1156
2.8852
6.1316
6.7445
4.0630
5.3683
4.4919
4.6680
4.9191
6.6983
3.6279
4.4622
8.2164
2.7894
4.0169
4.4953
2.7763
7.5153
2.6075
4.3347
d =
68.1908
-2.6819
77.8837
-55.4805
-84.0382
35.1940
-46.2358
41.1871
-14.6125
39.0071
-51.3673
5.4525
40.5083
1.8079
-47.9969
-66.9370
44.2995
53.3695
-88.8573
-50.4009
d =
68.1908
357.3181
77.8837
304.5195
275.9618
35.1940
313.7642
41.1871
345.3875
39.0071
308.6327
5.4525
40.5083
1.8079
312.0031
293.0630
44.2995
53.3695
271.1427
309.5991
>> X = ['The resultant force is', num2str(c),'kN and directed',num2str(d),'degrees measured counterclockwise from the positive x-axis.'];
disp(X)
Error using horzcat
Dimensions of arrays being concatenated are not consistent.

Answers (1)

VBBV
VBBV on 15 Nov 2022
Edited: VBBV on 15 Nov 2022
try with sprintf
a = [-3.821; 0.135; 5.995; -5.557; -4.041; -3.094; -3.244; -3.074; -1.241; -4.216; -2.834; -0.424; 5.337; -0.088; 2.985; 4.136; 1.939; 6.031; -2.607; 3.340];
b = [-1.529; -2.882; 1.287; 3.822; 0.422; -4.387; 3.107; -3.513; 4.760; -5.205; 2.265; -4.442; 6.247; -2.788; -2.688; -1.761; 1.987; 4.484; 0.052; -2.763];
c = (a.^2 + b.^2).^(1/2);
d = atand(a./b);
e = d;
d(d<0)=360+d(d<0);
X = sprintf('The resultant force is %0.1f kN and directed %0.1f degrees measured counterclockwise from the positive x-axis\n',c,d)
X =
'The resultant force is 4.1 kN and directed 2.9 degrees measured counterclockwise from the positive x-axis The resultant force is 6.1 kN and directed 6.7 degrees measured counterclockwise from the positive x-axis The resultant force is 4.1 kN and directed 5.4 degrees measured counterclockwise from the positive x-axis The resultant force is 4.5 kN and directed 4.7 degrees measured counterclockwise from the positive x-axis The resultant force is 4.9 kN and directed 6.7 degrees measured counterclockwise from the positive x-axis The resultant force is 3.6 kN and directed 4.5 degrees measured counterclockwise from the positive x-axis The resultant force is 8.2 kN and directed 2.8 degrees measured counterclockwise from the positive x-axis The resultant force is 4.0 kN and directed 4.5 degrees measured counterclockwise from the positive x-axis The resultant force is 2.8 kN and directed 7.5 degrees measured counterclockwise from the positive x-axis The resultant force is 2.6 kN and directed 4.3 degrees measured counterclockwise from the positive x-axis The resultant force is 68.2 kN and directed 357.3 degrees measured counterclockwise from the positive x-axis The resultant force is 77.9 kN and directed 304.5 degrees measured counterclockwise from the positive x-axis The resultant force is 276.0 kN and directed 35.2 degrees measured counterclockwise from the positive x-axis The resultant force is 313.8 kN and directed 41.2 degrees measured counterclockwise from the positive x-axis The resultant force is 345.4 kN and directed 39.0 degrees measured counterclockwise from the positive x-axis The resultant force is 308.6 kN and directed 5.5 degrees measured counterclockwise from the positive x-axis The resultant force is 40.5 kN and directed 1.8 degrees measured counterclockwise from the positive x-axis The resultant force is 312.0 kN and directed 293.1 degrees measured counterclockwise from the positive x-axis The resultant force is 44.3 kN and directed 53.4 degrees measured counterclockwise from the positive x-axis The resultant force is 271.1 kN and directed 309.6 degrees measured counterclockwise from the positive x-axis '
  2 Comments
Debbie
Debbie on 15 Nov 2022
Hi! Thank you for answering. But may I ask why the supposed values for "d" changes in the sentence? Thank you.
VBBV
VBBV on 15 Nov 2022
Edited: VBBV on 15 Nov 2022
a = [-3.821; 0.135; 5.995; -5.557; -4.041; -3.094; -3.244; -3.074; -1.241; -4.216; -2.834; -0.424; 5.337; -0.088; 2.985; 4.136; 1.939; 6.031; -2.607; 3.340];
b = [-1.529; -2.882; 1.287; 3.822; 0.422; -4.387; 3.107; -3.513; 4.760; -5.205; 2.265; -4.442; 6.247; -2.788; -2.688; -1.761; 1.987; 4.484; 0.052; -2.763];
c = (a.^2 + b.^2).^(1/2);
d = atand(a./b);
e = d;
d(d<0)=360+d(d<0);
for k = 1:length(d)
X = sprintf('The resultant force is %0.1f kN and directed %0.1f degrees measured counterclockwise from the positive x-axis\n',c(k),d(k))
end
X =
'The resultant force is 4.1 kN and directed 68.2 degrees measured counterclockwise from the positive x-axis '
X =
'The resultant force is 2.9 kN and directed 357.3 degrees measured counterclockwise from the positive x-axis '
X =
'The resultant force is 6.1 kN and directed 77.9 degrees measured counterclockwise from the positive x-axis '
X =
'The resultant force is 6.7 kN and directed 304.5 degrees measured counterclockwise from the positive x-axis '
X =
'The resultant force is 4.1 kN and directed 276.0 degrees measured counterclockwise from the positive x-axis '
X =
'The resultant force is 5.4 kN and directed 35.2 degrees measured counterclockwise from the positive x-axis '
X =
'The resultant force is 4.5 kN and directed 313.8 degrees measured counterclockwise from the positive x-axis '
X =
'The resultant force is 4.7 kN and directed 41.2 degrees measured counterclockwise from the positive x-axis '
X =
'The resultant force is 4.9 kN and directed 345.4 degrees measured counterclockwise from the positive x-axis '
X =
'The resultant force is 6.7 kN and directed 39.0 degrees measured counterclockwise from the positive x-axis '
X =
'The resultant force is 3.6 kN and directed 308.6 degrees measured counterclockwise from the positive x-axis '
X =
'The resultant force is 4.5 kN and directed 5.5 degrees measured counterclockwise from the positive x-axis '
X =
'The resultant force is 8.2 kN and directed 40.5 degrees measured counterclockwise from the positive x-axis '
X =
'The resultant force is 2.8 kN and directed 1.8 degrees measured counterclockwise from the positive x-axis '
X =
'The resultant force is 4.0 kN and directed 312.0 degrees measured counterclockwise from the positive x-axis '
X =
'The resultant force is 4.5 kN and directed 293.1 degrees measured counterclockwise from the positive x-axis '
X =
'The resultant force is 2.8 kN and directed 44.3 degrees measured counterclockwise from the positive x-axis '
X =
'The resultant force is 7.5 kN and directed 53.4 degrees measured counterclockwise from the positive x-axis '
X =
'The resultant force is 2.6 kN and directed 271.1 degrees measured counterclockwise from the positive x-axis '
X =
'The resultant force is 4.3 kN and directed 309.6 degrees measured counterclockwise from the positive x-axis '

Sign in to comment.

Categories

Find more on Loops and Conditional Statements 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!