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

2 views (last 30 days)
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!

Answers (2)

Torsten
Torsten on 12 Jan 2023
Edited: Torsten on 12 Jan 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 on 12 Jan 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.

Categories

Find more on Structures in Help Center and File Exchange

Products


Release

R2022b

Community Treasure Hunt

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

Start Hunting!