0

I have a python function, Foo, which returns a tuple of 3 lists. Each list contains points I would like to manipulate and plot in matlab. Current I can retrieve my data with

data = py.Foo.main(args)

Plotting the output proved difficult. Matlab sees "data" as a python object. I was previously in a rush and my quick work around was

function [r] = convertToVector(c)
%make sure everything is numeric so I can plot it
r = [];
for i = 1:length(c)
    if isnumeric(c{i})
        r = [r c{i}];
    end
end

filler = data.cell()
a = convertToVector(filler{1}.cell())
b = convertToVector(filler{1}.cell())
c = convertToVector(filler{1}.cell())

This works okay but for c I have something like

filler.cell{3}.cell = [0] [-1] [-1] [-1] [0.22] [0.13] [0.08]

When I run convertToVector everything is either 0 or -1. I have no idea what is going on. If I play around in the console I can get something like

r = [];
z = filler.cell{3}.cell;
r = [r z{1}]      >>> r = 0
r = [r z{2}]      >>> r = 0 -1
r = [r z{5}]      >>> r = 0 -1 0

Why is this happening? If I replace z{1} z{2} z{5} with 0 -1 0.22 respectively, I get the results I would expect: r = 0 -1 0.22. I don't use matlab regularly and I have no idea what may cause this. I was trying to plug the results of an analysis done in python into a pre-existing matlab gui, otherwise I would avoid interfacing the two as such.

Edit: Under further inspection utilizing whos r the class is int64 when adding z{1} to z{4} first and double when adding z{5} first. The class then changes from double to int64 if I add z{1} to z{4}

1 Answer 1

1

As per your edit, it seems the problem is that the initial integers in each cell (which are stored as ints or maybe numpy.uint64 types in python) are not automatically cast to double, and this seems reasonable. If you concatenate doubles to the uint64 array, the doubles will be cast to uint64 and not the other way around. This is only surprising since MATLAB very heavily defaults to using doubles whenever possible, so you might not expect this.

Simple python-free demonstration:

a = uint64([3, 4, 5]);
b = [a, 6];
c = [b, 7.3];

after this all three arrays will have uint64 class. Well, the example is a bit redundant, since class(6) is double, so if there was any typecasting, it would've already happened for b.

The solution is simple: explicitly cast to double in your function,

function [r] = convertToVector(c)
%make sure everything is numeric so I can plot it
r = [];
for i = 1:length(c)
    if isnumeric(c{i})
        r = [r double(c{i})];  %// only change here
    end
end
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.