Why do I get a complex number when I use 'uitable' in MATLAB?

82 views Asked by At

I have data file included both type of numbers, real (fractions and integers) and complex. But when I use 'uitable' all data will turn to be complex (of course 0i as imaginary part). see the figure Any recommendation on to get rid of those automatically appeared imaginary parts?

1

There are 1 answers

0
John Bofarull Guix On BEST ANSWER

there's a way around.

1.- Let's say that your data is for instance

c2 =   1.0e+02 *[  0.997200000000000 + 0.000000000000000i -2.980950000000000 - 2.615070000000000i  -5.201680000000000 - 5.166180000000001i -1.654660000000000 + 8.841020000000000i
 -7.100910000000000 - 0.214950000000000i  0.264990000000000 + 0.000000000000000i    -7.533630000000000 - 1.921760000000000i -9.006920000000001 + 0.000000000000000i
  7.060630000000000 + 0.000000000000000i -1.963840000000000 + 5.605040000000000i   -6.321850000000000 + 0.000000000000000i  8.054330000000000 + 1.504170000000000i
  2.441100000000000 + 8.001080000000000i -8.480670000000000 - 2.205220000000000i   -5.200950000000001 - 7.360539999999999i  8.895750000000000 - 8.804410000000001i]

then

hf1 = uifigure;
uit = uitable(hf1,'Data',c2);

enter image description here

when one tries casting or directly writing null imaginary part elements you are right, the table shown with uitable keeps 0i on all elements of the table as soon as a single element has non null imaginary part. The following doesn't work

uit.Data(1,1)

uit.Data(1,1)=real(uit.Data(1,1))

uit.Data(1,1)=49

if imag(c2(:))==0 c(:)=real(c(:)); end

enter image description here

2.- Now let's convert the data fed to uitable to characters

c10={};
for k=1:1:numel(c2)
c10=[c10 strip(num2str(c2(k)))];

end
c10=reshape(c10,size(c2));

hf2=uifigure;
uit2=uitable(hf2,'Data',c10)

enter image description here

Now the resulting table produced with uitable complies with the requirement in your question, that table elements with null imaginary parts do not show 0i.

Additional comment : I have used the following to generate random 4x4

simulating data
a=1e-3*randi([-1e6 1e6],1,16);
b=1e-3*randi([-1e6 1e6],1,16);
b(unique(randi([1 16],1,randi([4 12],1,1))))=0;
c=a+b*1j;
c2=reshape(c,[4 4])