Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8940619
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T11:04:50+00:00 2026-06-15T11:04:50+00:00

I have a structure s.a = [1 2 3]; s.b = [2 3 4

  • 0

I have a structure

s.a = [1 2 3];
s.b = [2 3 4 5];
s.c = [9, 6 ,3];
s.d = ... % etc. - you got the gist of it

Now I want to apply a function/operation on the data stored in each field and modify the content of the field, that is I want to apply

s.a = myFun( s.a );
s.b = myFun( s.b );
s.c = myFun( s.c ); % etc. ...

How can I do it without explicitly write all the fields as above?
I was thinking of structfun – but I’m not so sure how to accomplish this “in place” modification…

Thanks!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-15T11:04:52+00:00Added an answer on June 15, 2026 at 11:04 am

    For the impatient reader, the structfun solution is at the bottom of my answer 🙂 But I would first ask myself…

    What’s wrong with using a loop? The following example shows how it can be done:

    %# An example structure
    S.a = 2;
    S.b = 3;
    
    %# An example function
    MyFunc = @(x) (x^2);
    
    %# Retrieve the structure field names
    Names = fieldnames(S);
    
    %# Loop over the field-names and apply the function to each field
    for n = 1:length(Names)
        S.(Names{n}) = MyFunc(S.(Names{n}));
    end
    

    Matlab functions such as arrayfun and cellfun typically are slower than an explicit loop. I’m guessing structfun probably suffers from the same problem, so why bother?

    However, if you insist on using structfun it can be done as follows (I made the example a little more complicated just to emphasize the generality):

    %# structfun solution
    S.a = [2 4];
    S.b = 3;
    MyFunc = @(x) (x.^2);
    S = structfun(MyFunc, S, 'UniformOutput', 0);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have an Xcode project with a directory structure like this: MasterProjectDir/projectname.xcodeproj MasterProjectDir/ProjectSubDir/whatever.c MasterProjectDir/ProjectSubDir/etc.c
I'm trying to implement a generic notification system.. I have a data structure like
I've got a page where I have a ModalPopUpExtender which I want to show
I'd like to structure my website like this: domain.com/person/edit/1 domain.com/person/edit/2 domain.com/person/edit/3 etc. I have
I've got a data structure which consists of linked nodes. You can think of
I've got an XML structure bound to a WPF form, and have it all
I've got a bunch of nested divs, ul's etc. Structure is shown here. <script>
I have an existing database with existing data that I can't change it's structure
I have an abstract class Engine3D and it's structures ( Vector3D , etc.) in
I have structure like this. <ul class=gallerylist> <li> <image ...> <input ...> </li> <li>

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.