Using strings as an argument of a function

29 visualizaciones (últimos 30 días)
Fatih Enes Kose
Fatih Enes Kose el 22 de Mzo. de 2020
Comentada: Fatih Enes Kose el 22 de Mzo. de 2020
I want to write a function that takes 3 matrices and 2 strings as an argument. I successfully passed matrices as an argument but i could not do the same thing for strings. Is there anyone who knows how i can use strings as an argument and how should i pass strings on the line that i call the function? I want to use the function several times and i have different names for xlabel and ylabel so i want to use these names as an argument.
function [ ] = plotAmplitudeSpectrum( inp_signal, time_vector, Fsample, xlabel, ylabel )
% Fsample: sampling frequency
L = length(time_vector);
Y = fft(inp_signal);
P2 = abs(Y/L);
P1 = P2(1:L/2+1);
P1(2:end-1) = 2*P1(2:end-1);
f = Fsample*(0:(L/2))/L;
plot(f,P1,'LineWidth',2);
title('Sinyalin Genlik Spektrumu');
xlabel(xlabel);
ylabel(ylabel);
end

Respuesta aceptada

Sriram Tadavarty
Sriram Tadavarty el 22 de Mzo. de 2020
Editada: Sriram Tadavarty el 22 de Mzo. de 2020
Hi,
The issue is with the input names. Change the input names with variable names other than MATLAB inbuilt functions, You can place input to be xlabelstr, ylabelstr and then use them in the function as such:
function [ ] = plotAmplitudeSpectrum( inp_signal, time_vector, Fsample, xlabelstr, ylabelstr )
%...
xlabel(xlabelstr)
ylabel(ylabelstr)
You could directly pass as such:
plotAmplitudeSpectrum( inp_signal, time_vector, Fsample, 'X Label', 'y label' )
% Or even
plotAmplitudeSpectrum( inp_signal, time_vector, Fsample, "X Label", "y label")
Hope this helps.
Regards,
Sriram
  3 comentarios
Sriram Tadavarty
Sriram Tadavarty el 22 de Mzo. de 2020
Do accept the answer, if helpful.
Fatih Enes Kose
Fatih Enes Kose el 22 de Mzo. de 2020
I did, thanks again

Iniciar sesión para comentar.

Más respuestas (0)

Categorías

Más información sobre Logical en Help Center y File Exchange.

Community Treasure Hunt

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

Start Hunting!

Translated by