"Too many output arguments" for non-outputting function
3 Ansichten (letzte 30 Tage)
Ältere Kommentare anzeigen
Matlab novice here; my previous programming experience is mostly with C++ and Java. I have a college assignment to use Matlab to play some music, and I've hit a snag in a function I'm trying to write to speed things up. I composed the alto staff of the first line of the song, but when I went to test-run the file to see if I was on the right track, I got an error message that I had too many output arguments... for a function that outputs no arguments (a void function in C++ parlance).
My current code (less the introductory comments):
% Misc Global Variables
sf = 2000; % sampling frequency
% Note frequencies (Hz) - GLOBAL CONSTANTS
% middleC = 261.63; % middle C included as reference point; commented out
% b/c highest note in refrain is an A flat
D = 293.66;
E = 329.63;
F = 349.23;
G = 392.00;
Aflat = 415.30; % relevant staff is marked with a flat on the A space
% Song playback
note(G, 0.1); % I
note(G, 0.1); % was
note(G, 0.2); % cho-
note(F, 0.1); % -sen
note(E, 0.1); % by
note(F, 0.2); % heav-
note(D, 0.3); % -en
function note(pitch, duration)
% NOTE Local function that outputs a note for playback based on an input
% pitch (i.e. frequency) and duration
t = 1:1/sf:duration;
n = sin(2 * pi * pitch * t);
sound(n, sf);
end
The error message is on line 41, the line that says t = 1:1/sf:duration;. The intent of the function is just to save me having to type sound(note(pitch, duration), sf); a gazillion times by letting me just type note(pitch, duration); to get playback. But it doesn't work even if I move sound() out of the note() function.
Using Matlab R2020b.
0 Kommentare
Antworten (1)
Walter Roberson
am 4 Nov. 2020
t = 1:1/sf:duration;
You are inside a non-nested function at that point. sf is not defined as a variable in that context, so you are getting the function sf which is a function that invokes StateFlow .
3 Kommentare
Walter Roberson
am 4 Nov. 2020
MATLAB uses the keyword global to denote global variables.
MATLAB has a "base workspace" that is used at the command line and in scripts called from the command line (or inside such scripts), or which can be deliberately accessed using evalin('base')
However variables in the base workspace are not automatically imported into functions. You lose a lot of optimization opportunities if you have to resolve all undefined variables at run-time against the base workspace.
Steven Lord
am 4 Nov. 2020
Functions operate in their own workspaces to reduce the programmer's need to worry if someone else create a variable with the same name they use that could then break their code. If you want to make a variable globally accessible you have to explicitly tell MATLAB that using the global keyword, as Walter stated. By making that declaration you're opting into depending on a variable that anyone with access to the global workspace could change and affect the behavior of your code.
Siehe auch
Produkte
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!