I cannot give this struct as input of a custom function, how can I do?

3 visualizzazioni (ultimi 30 giorni)
Good evening to all,
I was wondering if it is possible to give this structure as input of a custom function:
"hold_on" : 1
"box_on" : 1
"grid_on" : 0
"grid_minor" : 0
because I always get this error: Undefined function 'myfunction' for input arguments of type 'struct'
Can someone help me?
Thank you in advance!

Risposte (2)

Torsten
Torsten il 12 Gen 2023
Modificato: Torsten il 12 Gen 2023
Works without problems.
Have you saved an m-file named "myfunction.m" in your working directory ?
var.hold_on = 1;
var.box_on = 1;
var.grid_on = 0;
var.grid_minor = 0;
class(var)
ans = 'struct'
myfunction(var)
hold_on = 1
box_on = 1
grid_on = 0
grid_minor = 0
function [] = myfunction(var)
hold_on = var.hold_on
box_on = var.box_on
grid_on = var.grid_on
grid_minor = var.grid_minor
end

Steven Lord
Steven Lord il 12 Gen 2023
It is possible to write a function that accepts a struct array as input. Without seeing the body of your myfunction function we can't be certain of the cause of the error message you received, but if I had to guess I'd guess that your myfunction function was not defined as the first function in a file named myfunction.m.
If the file name and the function name differ, you will need to use the file name to call the function. So if "function myfunction(x)" was in a file named pizza.m you would have to call it by the name pizza.
Only the first function in a function file is directly callable by users outside that file.

Categorie

Scopri di più su Structures in Help Center e File Exchange

Prodotti


Release

R2022b

Community Treasure Hunt

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

Start Hunting!

Translated by