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

Community Tip - If community subscription notifications are filling up your inbox you can set up a daily digest and get all your notifications in a single email. X

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

bsatola
4-Participant

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

Screenshot 2022-12-02 113828.png

Illustrative file attached.

1 ACCEPTED SOLUTION

Accepted Solutions

Why not simply

Werner_E_0-1670017720965.png

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:

Werner_E_1-1670017875790.png

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:

Werner_E_2-1670017964943.png

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

Werner_E_5-1670018235704.png

 

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!

 

 

View solution in original post

3 REPLIES 3
bsatola
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

 

Screenshot 2022-12-02 121327.png

Why not simply

Werner_E_0-1670017720965.png

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:

Werner_E_1-1670017875790.png

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:

Werner_E_2-1670017964943.png

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

Werner_E_5-1670018235704.png

 

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!

 

 

bsatola
4-Participant
(To:Werner_E)

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

Top Tags