Making two data appear side-to-side

I have two variable: array and Sw_C that i need to present side-to-side as in the picture attached and I'm using fprintf to do so
but it seems like something is wrong in the fprintf that I'm using at LINE 19. Hope my question make sense and clear to you and please help me to check and correct it. Thank you. The excel data is attached here as well.
*note that in the Rt data in the picture is defined as variable "array" in the script to avoid any confusion
syms Sw
format short
p=0.153;
Qv=0.980;
Rw=0.265;
m=1.86;
B=3.2;
n=2.2;
array=xlsread('watersat','Sheet1','A2:A45');
Sw_Lab=xlsread('watersat','Sheet1','B2:B45');
for i=1:length(array)
Rt=array(i);
eq=Sw==(Rw./((p^m)*Rt.*(1+(B*Qv*Rw)/Sw))).^(1/n);
sol=vpasolve(eq,Sw);
a(i)=sol;
end
Sw_C=a';
fprintf('\n%2.4s\t%2.4s\n','array','Sw_C');
fprintf('%2.4f\t%2.4f\n',array,Sw_C)

 Accepted Answer

Try this:
fprintf('%2.4f\t%2.4f\n',[array(:),Sw_C(:)].')

2 Comments

Hello sir, thank you for the help. I tried it but it didn't really work as shown under the script.
*note that I've renamed the variable "array" to "Rt"
syms Sw
format short
p=0.153;
Qv=0.980;
Rw=0.265;
m=1.86;
B=3.2;
n=2.2;
Rt=xlsread('watersat','Sheet1','A2:A45');
Sw_Lab=xlsread('watersat','Sheet1','B2:B45');
for i=1:length(Rt)
Rteqn=Rt(i);
eq=Sw==(Rw./((p^m)*Rteqn.*(1+(B*Qv*Rw)/Sw))).^(1/n);
sol=vpasolve(eq,Sw);
a(i)=sol;
end
Sw_C=a';
fprintf('\n%2.4s\t%2.4s\t%2.4s\n','Rt','Sw_C');
fprintf('%2.4f\t%2.4f\n',[Rt(:),Sw_C(:)].')
It turns out to look this way:
>> watersat
Rt Sw_C 4.0545 1.0947
4.0950 1.0886
4.2167 1.0707
4.2978 1.0592
4.3789 1.0480
4.5410 1.0265
4.8249 0.9915
5.0276 0.9683
5.2303 0.9465
5.4330 0.9260
5.6358 0.9065
5.7979 0.8917
6.2439 0.8539
6.4872 0.8349
6.6088 0.8259
6.7305 0.8170
6.7305 0.8170
6.6899 0.8200
7.0143 0.7973
7.5008 0.7662
7.7846 0.7495
8.1495 0.7292
8.2712 0.7227
8.4739 0.7123
9.2443 0.6758
9.9741 0.6453
10.5820 0.6223
10.9470 0.6094
11.3930 0.5946
11.6360 0.5869
12.0010 0.5757
12.4070 0.5639
13.4610 0.5359
13.9070 0.5250
15.2040 0.4962
19.2830 0.4259
21.0830 0.4018
22.8270 0.3814
24.7730 0.3612
26.2330 0.3477
28.0570 0.3324
30.1650 0.3165
31.0170 0.3106
31.5030 0.3073
For some reason you have written the first fprintf format string with three conversion specifiers, but you have only provided two inputs to fprintf. This means that fprintf will simply stop after those two inputs have been processed, and does continue with the rest of the format string. The upshot is that the newline at the end does not get printed.
The format string that you showed in your question is correct. It is not clear why you made that change.
fprintf('\n%2s\t%2s\n','Rt','Sw_C');
Tip: if you want the columns to be aligned then you should change the field width to something like 8 or 10.
I am happy to help defining suitable format strings, but you will need to make a comment showing the exact output that you would like to achieve, correctly formatted as code.

Sign in to comment.

More Answers (0)

Asked:

on 8 Jun 2020

Edited:

on 8 Jun 2020

Community Treasure Hunt

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

Start Hunting!