How can I use a structure as an input variable for a fuction?

1 view (last 30 days)
I have a structure with five fields full of data. I want to put this structure into a function, manipulate the fields, and output the structure again. How do I do this? I'm getting an error-
[trialslopes(iC)] = NO2xavecals(trialslopes(iC));
Error: File: NO2xavecals.m Line: 63 Column: 48
Expression or statement is incorrect--possibly unbalanced (, {, or [.
What am I missing in my notation? How can I make this work?
--------edit- more information -------------
The line before the function call defines the fields of trialslopes(iC):
trialslopes(iC).NO2day = stuff1;
trialslopes(iC).NO2on = stuff2;
trialslopes(iC).NO2off = stuff3;
trialslopes(iC).NO2chop = stuff4;
trialslopes(iC).NO2err = stuff5;
Then the function call:
[trialslopes(iC)] = NO2xavecals(trialslopes(iC));
The function code is:
function trialstruct = NO2xavecals(trialstruct)
fns = fieldnames(trialstruct);
trialday = trialstruct.(fns{1});
trialon = trialstruct.(fns{2});
trialoff = trialstruct.(fns{3});
trialchop = trialstruct.(fns{4});
trialerr = trialstruct.(fns{5});
Then math is done on the variables and it all returned to the structure:
trialstruct.(fns{1}) = daymean;
trialstruct.(fns{2}) = onmean;
trialstruct.(fns{3}) = offmean;
trialstruct.(fns{4}) = chopmean;
trialstruct.(fns{5}) = errmean;
end
When I run the code, however, I get this error:
Error: File: NO2xavecals.m Line: 63 Column: 48
Expression or statement is incorrect--possibly unbalanced (, {, or [.
Error in NO2x_prep_cals (line 316)
[trialslopes(iC)] = NO2xavecals(trialslopes(iC));
I must be missing something about using structures as inputs into functions, but I can't find an answer in the forums.
Trialslopes is a structure defined as so:
trialslopes =
NO2day: [1x53 double]
NO2on: [1x53 double]
NO2off: [1x53 double]
NO2chop: [1x53 double]
NO2err: [1x53 double]
Thanks
Thanks.

Answers (2)

Star Strider
Star Strider on 10 Oct 2014
We need to see more of your code. The error may not be in that line but in the line just before it. At least that’s been my experience in situations where my code has thrown that error and I couldn’t find it in the line that threw the error.
  3 Comments
Star Strider
Star Strider on 11 Oct 2014
Edited: Star Strider on 13 Oct 2014
I’m not certain you’ve posted:
NO2xavecals.m Line: 63
and the line above it.
That seems to be throwing the error, not the function call itself.
EDIT — What specifically is:
NO2xavecals.m Line: 63
We still don’t know. Please quote that line and the line before it in their entirety if you want our help on it.
Megan
Megan on 13 Oct 2014
Edited: Megan on 13 Oct 2014
Thanks, I just found a small, stupid error in my code that hopefully fixes everything... Thanks for responding. I'm going to remove my original question now, i if I can figure out how...

Sign in to comment.


Image Analyst
Image Analyst on 13 Oct 2014
Why are you using dynamic field names when you don't have to? Try simplifying the code with this:
trialday = trialstruct.NO2day;
trialon = trialstruct.NO2on;
trialoff = trialstruct.NO2off;
trialchop = trialstruct.NO2chop;
trialerr = trialstruct.NO2error;
trialstruct.NO2day = daymean;
trialstruct.NO2on = onmean;
trialstruct.NO2off = offmean;
trialstruct.NO2chop = chopmean;
trialstruct.NO2error = errmean;
Since you already know the field names in advance, just use them. Perhaps that's where your errir is coming it - I don't know.
Also, when you call, you don't really need the brackets on the left hand side:
trialslopes(iC) = NO2xavecals(trialslopes(iC));

Categories

Find more on Get Started with MATLAB in Help Center and File Exchange

Products

Community Treasure Hunt

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

Start Hunting!