How do I access a field from a function's input?
2 visualizzazioni (ultimi 30 giorni)
Mostra commenti meno recenti
Andy Garcia Barrabeitg
il 10 Gen 2022
Commentato: Andy Garcia Barrabeitg
il 10 Gen 2022
Hello, I currently have a struct with different field names "Delta", "Theta", "Beta", and "Gamma". I'd like to be able to call upon these different fields in my function based on some input "freq" (sample code below). However, whenever I try to access the field from the variable it takes the variable as a literal value "freq" instead of the input in the function and thus I get an error that it can't find myStruct.freq. Is there a way I could go around this? Thanks!
function[output] = myFunction(freq)
output = myStruct.freq
end
0 Commenti
Risposta accettata
James Tursa
il 10 Gen 2022
Modificato: James Tursa
il 10 Gen 2022
Assuming you are passing in a character string for freq, use this syntax:
output = myStruct.(freq);
3 Commenti
James Tursa
il 10 Gen 2022
You need to pass in the myStruct variable to the function also. But, quite frankly, if that is all the function does in your actual case I wouldn't even bother with the function at all. Just use the appropriate syntax in your calling code and avoid the function all together.
Più risposte (0)
Vedere anche
Categorie
Scopri di più su Structures in Help Center e File Exchange
Prodotti
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!