Pass a Structure and Structure Elements to a Function

조회 수: 2 (최근 30일)
Franco
Franco 2013년 3월 2일
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
Franco
Franco 2013년 3월 2일
Say that the structure is of the form Electrical.Year.Month. I would like to pass the structure Electrical to the function. Specify the Year and the Day. So for example, if I want to do an analysis on the data set in Electrical.Twelve.January, I would like to pass that on to the function in the form monthlyplot(Electrical, 'Twelve','January'). When I do that though,t he error I get is
monthlyplot(Electrical,'Twelve','January')
Reference to non-existent field 'year'.
Azzi Abdelmalek
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
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);

추가 답변 (1개)

Azzi Abdelmalek
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

카테고리

Help CenterFile Exchange에서 Structures에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by