cancel
Showing results for
Did you mean:
cancel
Showing results for
Did you mean:

Community Tip - New to the community? Learn how to post a question and get help from PTC and industry experts! X

4-Participant

## Trouble exporting function results to Excel, keeps saying "This value must be a scalar or a matrix"

Illustrative file attached.

1 ACCEPTED SOLUTION

Accepted Solutions
24-Ruby V
(To:bsatola)

Why not simply

The problem in fact is not the function but its argument. i is a range and a range is NOT a vector but rather some sort of implicit loop. So f(i) is neither a range nor a vector - its kind of an invalid object suitable only for display. You can see this if you try to assign f(i) a variable:

But what you did works OK if you turn "i" into a true vector. The simplest way to do so is by using an immediate inline evaluation after the definition:

But that method of turning a range into a vector is undocumented and so is subject to be changed without notice.

So a more "legal" way could be this

Note that I use vectorization when I call f(v).
You will see why when you change the x^2 to x*x in the function definition and don't use vectorization 🙂

BTW, ranges should only be used on three occasions:

1) to index vectors and matrices

2) to provide a range of values for the abscissa in a 2D plot

3) in a program when you use a for-loop

Nothing else!

3 REPLIES 3
4-Participant
(To:bsatola)

I just figured it out. I had to create a new function that returned an array. A bit uglier now in my opinion. I mean the original function returned the same matrix and values (see below) but does not work? Silly IMHO

24-Ruby V
(To:bsatola)

Why not simply

The problem in fact is not the function but its argument. i is a range and a range is NOT a vector but rather some sort of implicit loop. So f(i) is neither a range nor a vector - its kind of an invalid object suitable only for display. You can see this if you try to assign f(i) a variable:

But what you did works OK if you turn "i" into a true vector. The simplest way to do so is by using an immediate inline evaluation after the definition:

But that method of turning a range into a vector is undocumented and so is subject to be changed without notice.

So a more "legal" way could be this

Note that I use vectorization when I call f(v).
You will see why when you change the x^2 to x*x in the function definition and don't use vectorization 🙂

BTW, ranges should only be used on three occasions:

1) to index vectors and matrices

2) to provide a range of values for the abscissa in a 2D plot

3) in a program when you use a for-loop

Nothing else!

4-Participant
(To:Werner_E)

Fantastic! Thank you!! That's much better 😄

Announcements
Top Tags