Pass a Structure and Structure Elements to a Function
2 次查看(过去 30 天)
显示 更早的评论
I need to pass on a Structure as well as the elements of that structure to a function, but I keep on getting an error. Anyone out there know how to fix this one. Thanks in advance.
function [output] = monthlyplot(data,year,month);
%data; pass on structure; ex. Electrical
%year; pass on year of interest; ex. Twelve
%month; pass on month of interest; ex. January
month2 = struct('January',1,'February',2,'March',3,'April',4,'May',5,'June',6,'July',7,'August',8,'September',9,'October',10,'November',11,'December',12);
year2 = struct('Twelve',1,'Thirteen',2);
fieldmonths = fieldnames(month2);
fieldyear = fieldnames(year2);
temp1 = getfield(data.year.month);
3 个评论
Azzi Abdelmalek
2013-3-3
编辑:Azzi Abdelmalek
2013-3-3
You can use
Electrical.('twelve').('January')
%or
year='twelve'
month='January'
Electrical.(year).(month)
采纳的回答
Walter Roberson
2013-3-3
function [output] = monthlyplot(data,year,month);
%data; pass on structure; ex. Electrical
%year; pass on year of interest; ex. Twelve
%month; pass on month of interest; ex. January
temp1 = data.(year).(month);
0 个评论
更多回答(1 个)
Azzi Abdelmalek
2013-3-2
编辑:Azzi Abdelmalek
2013-3-2
You do not need to pass the fields of your struct variable in function arguments.
function output= monthlyplot(data)
% Maybe you should give more informations
0 个评论
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Structures 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!