- Call LOAD with an output argument (which will be a struct array) and then define the variable peaks in terms of the fields of that output argument.
- Move your LOAD call outside your function and pass either the struct array from the call to LOAD or the variable you loaded on its own into your function as a third input argument. This has the added benefit of not performing file access each time your function is called, which may lead to significant savings (particularly if you call your function in a loop or pass it into a function like INTEGRAL that does so.)
Code fails to run in function, works in command window, works in debug "evaluate selection"
16 views (last 30 days)
I had an extremely infuriating problem. My code evaluates differently in the command window than in a function.
It fails on peak = interp1(wvcenter, peaks, wavelength);
wvcenter is a 33x1 double, peaks is a 33x1 double, wavelength is a double
It fails to run inside the function only. I can run the same code in the command window. I can run it by debugging with a stop on line 5, highlight and evaluate selection. But it fails if I use continue or step through code. It turns out peaks is an existing function that I have been storing a value to, but why does it evaluate differently? What is wrong?
function transmission = lctfTransmission(wavelength, wavelengths)
peak = interp1(wvcenter, peaks, wavelength);
bandwidth = interp1(wvcenter, bandwidths, wavelength);
wv2 = (wv1-mean(wv1))*bandwidth/max(wv1-mean(wv1))+wavelength;
t2 = t1*peak/max(t1);
transmission = interp1(wv2, t2, wavelengths, 'linear', 0);
and the error
Error using griddedInterpolant
The grid vectors do not define a grid of points that match
the given values.
Error in interp1 (line 158)
F = griddedInterpolant(Xext,V,method);
Error in lctfTransmission (line 5)
Steven Lord on 16 Oct 2015
You're "poofing" the variable named peaks into the workspace at runtime. However, when MATLAB parsed the function it needed to know to what the identifier 'peaks' referred. It can't "peek" into the MAT-file during parsing, so it can't tell that the MAT-file contains that variable, but it CAN see what is on the MATLAB path. Since there is a function named peaks on the path, it 'decides' that the identifier refers to that function and it can't change its mind at runtime.
To resolve this, do one of a few things: