Why Does MATLAB's interp1 Function Throw an Output Argument Error?

  • Thread starter Thread starter GreenPrint
  • Start date Start date
  • Tags Tags
    Matlab
Click For Summary

Discussion Overview

The discussion revolves around an error encountered when using MATLAB's interp1 function, specifically regarding output arguments. Participants explore the behavior of the function when attempting to assign multiple outputs to variables and clarify how outputs are handled in MATLAB.

Discussion Character

  • Technical explanation
  • Homework-related
  • Conceptual clarification

Main Points Raised

  • One participant describes receiving an output argument error when trying to assign two outputs from interp1 to variables, noting that the function runs without error when not assigning outputs.
  • Another participant explains that interp1 returns multiple outputs as a single vector, which can be assigned to a single variable, rather than multiple separate outputs.
  • A later reply acknowledges the clarification about output handling in MATLAB functions, indicating a misunderstanding about how outputs are structured.
  • Further discussion highlights that while multiple outputs are possible in MATLAB functions, interp1 only provides one output in its documentation, contrasting it with other functions like meshgrid that can return multiple outputs.

Areas of Agreement / Disagreement

Participants generally agree on the behavior of the interp1 function regarding output handling, but there is a recognition of differing assumptions about function outputs in MATLAB.

Contextual Notes

Participants note that the documentation for interp1 indicates a single output, which may lead to confusion for users expecting multiple outputs based on their assumptions or experiences with other functions.

GreenPrint
Messages
1,186
Reaction score
0
RESOLVED

Homework Statement


Hi,

Code:
>>v=[1;2;3;4;5;6];
P=[2494 4157;1247 2078;831 1386;623 1039;499 831;416 693];
[P_300,P_500]=interp1(v,P,5.2,'linear')

Error in ==> interp1 at 79
xOffset = 1;

? Output argument "varargout{2}" (and maybe others) not assigned during call to "C:\Program
Files\MATLAB2\R2011a\toolbox\matlab\polyfun\interp1.m>interp1".

I don't understand why I'm getting this error. I'm trying to assign the two values equal to variables. The code runs fine when I try not to assign the values to variables but I would like to assign the values to variables. I don't see what I'm doing wrong.

Code:
>> v=[1;2;3;4;5;6];
P=[2494 4157;1247 2078;831 1386;623 1039;499 831;416 693];
interp1(v,P,5.2,'linear')

ans =

  482.4000  803.4000

Thanks in advance!

Homework Equations


The Attempt at a Solution

 
Last edited:
Physics news on Phys.org
You get multiple outputs in a single vector, not [out0, out1, ...] = interp1(...)

Running the code:
Code:
v=[1;2;3;4;5;6];
P=[2494 4157;1247 2078;831 1386;623 1039;499 831;416 693];
Q = interp1(v,P,5.2,'linear')

returns:
Code:
>> Q

Q =

  482.4000  803.4000


When you are running:
Code:
>> v=[1;2;3;4;5;6];
P=[2494 4157;1247 2078;831 1386;623 1039;499 831;416 693];
interp1(v,P,5.2,'linear')

ans =

  482.4000  803.4000

The output (a vector) is stored in the variable "ans" and it is displayed since you are not using ";" to close the line.
 
That makes sense. I didn't realize functions output results into a single vector. I guess I just sort of assumed the opposite. It's something my book never informed me of. Thanks for letting me know.
 
yeah no prob.

You *can* have multiple outputs from a function. However, if you look at the comments for interp1 it only shows one output.

Contrast this to "help meshgrid" which has multiple outputs. As an example, you can call the meshgridfunction like:

[X,Y] = meshgrid(x,y) and it will return two outputs.

Note that these are all optional, and if outputs aren't specified, MATLAB throws the first output into the "ans" variable.