Text as input for function

9 Ansichten (letzte 30 Tage)
Hester Krommendijk
Hester Krommendijk am 11 Mär. 2020
Beantwortet: Ameer Hamza am 11 Mär. 2020
Hi, I need tot write a function 'tjirp' that takes an input 'linear' (so the text). If I do so, I get a parse error saying it's invalid MATLAB syntax. What am I missing?
function [x,info]=tjirp(t,f, 'lineair');
if t > 1
msg = 't moet kleiner zijn dan 1';
error(msg)
end
if 'lineair' == 'kwadratisch'
x = cos(2*pi*f*t.^3/3);
else
x = cos(2*pi*f*t.^2/2);
end
t = 0: 0.1:1;
plot(t, x)
title('Plot van cosinus')
xlabel('tijd')
if f>50
info = fprintf ('samplefrequentie is groot genoeg');
else
info = fprintf ('samplefrequentie is te klein');
end
end

Antworten (1)

Ameer Hamza
Ameer Hamza am 11 Mär. 2020
When defining a function, you need to write the name of a variable
function [x,info]=tjirp(t,f, str)
Also change the line
if str == 'kwadratisch'
You can pass the value value when calling the function
tjirp(2,1,'linear') % for example

Kategorien

Mehr zu Creating, Deleting, and Querying Graphics Objects finden Sie in Help Center und File Exchange

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by