Solution to IF statement please :)
4 Ansichten (letzte 30 Tage)
Ältere Kommentare anzeigen
Edmund Paul Malinowski
am 14 Nov. 2015
Kommentiert: Edmund Paul Malinowski
am 18 Nov. 2015
Hey all,
The basic start of my program i'm trying to write is to take user input of note values and create a wav file based upon these values but I'm having problems with parsing the user input and converting it to the particular frequency of that note..
Here is my code so far:
% SHOW INPUT DIALOG AND TAKE USER INPUT..
prompt={'1st Note','2nd Note','3rd Note','4th Note','5th Note','6th Note','7th Note'};
name='Enter Note Values (C-B)..';
num_lines=1;
%list = {'B'};
%options.Interpreter = 'tex';
defaultans={'C','D','E','F','G','A','B'};
answer=inputdlg(prompt,name,[1 50],defaultans);
% TEST IF USER FORGETS AN INPUT..
for x = 1:length(answer)
tf=isempty(answer);
if tf==1
msgbox('You missed a note(s) out..','Error','error','modal');
end
% END IF..
end
% END FOR..
% CONFIGURE INPUT TO VARIABLES..
Note1 = answer{1};
Note2 = answer{2};
Note3 = answer{3};
Note4 = answer{4};
Note5 = answer{5};
Note6 = answer{6};
Note7 = answer{7};
% LOOP THROUGH ANSWERS AND ASSIGN FREQUENCY VALUES..
for x = 1:length(answer)
if answer{x} == 'C'
FreqX = num2str(261.626);
elseif answer{x} == 'D'
FreqX = num2str(293.665);
elseif answer{x} == 'E'
FreqX = num2str(329.628);
elseif answer{x} == 'F'
FreqX = num2str(349.228);
elseif answer{x} == 'G'
FreqX = num2str(391.995);
elseif answer{x} == 'A'
FreqX = num2str(440.000);
elseif answer{x} == 'B'
FreqX = num2str(493.883);
end
% END IF..
end
% END FOR..
msgbox({'Freq1 is: ', FreqX, 'Hz' 'Note1 is: ', Note1});
msgbox({'Freq2 is: ', FreqX, 'Hz' 'Note2 is: ', Note2});
Now although there are no errors, its working as intended. The msgboxes are simple to help me see the results as i build my program but the frequency it displays is always the final one in the if statement.
I could do if statements for EACH of the answer{} array but this would be a lot of code so what would be the best way to iterate through the answers and assign the correct frequency values..
I can't get my head around the best and easiest way..
Any ideas?
Thanks,
Paul
0 Kommentare
Akzeptierte Antwort
Stephen23
am 14 Nov. 2015
Bearbeitet: Stephen23
am 14 Nov. 2015
Note that you should not use numbered variables, as this invariably leads beginners to dynamic variable naming, which is a very poor programming practice. Instead you should note that these numbers are essentially indices, in which case actually make them real indices. Read this to know why numbered variables are a bad idea:
And this to know why dynamically naming variable is an even worse idea:
Also using if's and loops is slow and inefficient: MATLAB works best when you learn how to write vectorized code, and to not use loops to solve every little task. Try this:
prompt={'1st Note','2nd Note','3rd Note','4th Note','5th Note','6th Note','7th Note'};
name='Enter Note Values (C-B)..';
defaultans={'C','D','E','F','G','A','B'};
answer=inputdlg(prompt,name,[1,50],defaultans);
% These three lines replace all of your loop and elseif statements:
V = 'CDEFGAB';
idx = cellfun(@(c)find(V==c),answer);
frq = [261.626,293.665,329.628,349.228,391.995,440.000,493.883];
out = frq(idx)
5 Kommentare
Weitere Antworten (0)
Siehe auch
Kategorien
Mehr zu Startup and Shutdown finden Sie in Help Center und File Exchange
Produkte
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!