calling a matlab function with scalar type

I am collecting data from a Simulink model into an array so I can manipulate it using a custom function in Matlab. The data is an array of dimensions 1601 x 1, single precision data. Now, I want to operate on one element of this data with my own function which is written in C and converted into a MEX function.
The function works fine with data entered like this:
x = 0.2345
myFunc(x)
...yet when I do this:
x = arr(100)
myFunc(x)
I get the error:
Error using myFunc
Input multiplier must be a scalar.
...however this
isscalar(x)
...yields this...
ans =
logical
1
...so is 'x' a scalar or not?
If not, how can I make it a scalar so I can call it with my function? Thanks.

6 件のコメント

Geoff Hayes
Geoff Hayes 2017 年 1 月 30 日
Richard - can you attach your myFunc so that we can see what it is doing? Perhaps the error message, Input multiplier must be a scalar. is incorrectly being written to the console and it should be some other error message instead.
Also, what is x equal to once you assign it as
x = arr(100)
Honglei Chen
Honglei Chen 2017 年 1 月 30 日
It would be helpful if you could share what's the result when you do
whos arr x
Richard Poley
Richard Poley 2017 年 1 月 30 日
Geoff, Thanks for your reply. The MEX function is attached inside the zip. It simply computes log2 with the input between 0 and 1. When I reference x as you wrote, I see:
x =
single
0.0941
Richard Poley
Richard Poley 2017 年 1 月 30 日
Honglei, Thanks.
The result is as follows:
Name Size Bytes Class Attributes
arr 1601x1 6404 single
x 1x1 4 single
Geoff Hayes
Geoff Hayes 2017 年 1 月 30 日
Richard - can you attach the C code instead? I would like to see that as opposed to the compiled Mex function. (Should be able to just attach the code rather than a zip file, even if you have to rename it with a txt extension.)
Richard Poley
Richard Poley 2017 年 1 月 30 日
Sorry, Geoff. It's proprietary so I'll need to check.
I'll post back when I know.

サインインしてコメントする。

 採用された回答

Geoff Hayes
Geoff Hayes 2017 年 1 月 30 日

0 投票

No worries, Richard. One other thing you may want to consider trying is to cast your x to a double. The example
x = 0.2345
class(x)
shows the x is a double and since this works (as expected) then you may want to try that for the other. For example,
x = double(arr(100));
myFunc(x)
Now, you will be passing in the same data type. The error message might be misleading and so we see Input multiplier must be a scalar rather than the "true" error message which might be "Input multiplier must be a double rather than a single". This is just a guess though! :)

1 件のコメント

Richard Poley
Richard Poley 2017 年 1 月 30 日
Geoff. That looks like it works for me. Thanks very much for your help.

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

製品

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by