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 133849
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T06:30:25+00:00 2026-05-11T06:30:25+00:00

I’ve often found myself doing something like this: unprocessedData = fetchData(); % returns a

  • 0

I’ve often found myself doing something like this:

unprocessedData = fetchData();  % returns a vector of structs or objects processedData = [];             % will be full of structs or objects  for dataIdx = 1 : length(unprocessedData)      processedDatum = process(unprocessedData(dataIdx));     processedData = [processedData; processedDatum]; end 

Which, whilst functional, isn’t optimal – the processedData vector is growing inside the loop. Even mlint warns me that I should consider preallocating for speed.

Were data a vector of int8, I could do this:

% preallocate processed data array to prevent growth in loop processedData = zeros(length(unprocessedData), 1, 'int8'); 

and modify the loop to fill vector slots rather than concatenate.

is there a way to preallocate a vector so that it can subsequently hold structs or objects?


Update: inspired by Azim’s answer, I’ve simply reversed the loop order. Processing the last element first forces preallocation of the entire vector in the first hit, as the debugger confirms:

unprocessedData = fetchData();  % note that processedData isn't declared outside the loop - this breaks  % it if it'll later hold non-numeric data. Instead we exploit matlab's  % odd scope rules which mean that processedData will outlive the loop % inside which it is first referenced:   for dataIdx = length(unprocessedData) : -1 : 1      processedData(dataIdx) = process(unprocessedData(dataIdx)); end 

This requires that any objects returned by process() have a valid zero-args constructor since MATLAB initialises processedData on the first write to it with real objects.

mlint still complains about possible array growth, but I think that’s because it can’t recognise the reversed loop iteration…

  • 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. 2026-05-11T06:30:25+00:00Added an answer on May 11, 2026 at 6:30 am

    Since you know the fields of the structure processedData and you know its length, one way would be the following:

    unprocessedData = fetchData(); processedData = struct('field1', [], ...                        'field2', []) % create the processed data struct processedData(length(unprocessedData)) = processedData(1); % create an array with the required length for dataIdx = 1:length(unprocessedData)     processedData(dataIdx) = process(unprocessedData(dataIdx)); end 

    This assumes that the process function returns a struct with the same fields as processedData.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 94k
  • Answers 94k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer You can use any plugin which allows editing database through… May 11, 2026 at 6:53 pm
  • Editorial Team
    Editorial Team added an answer getElementByID requires the HTML element to have an ID, just… May 11, 2026 at 6:53 pm
  • Editorial Team
    Editorial Team added an answer If that element has vertical padding or margin, it’s added… May 11, 2026 at 6:53 pm

Related Questions

I ran into a problem. Wrote the following code snippet: teksti = teksti.Trim() teksti
I am currently running into a problem where an element is coming back from
Seemingly simple, but I cannot find anything relevant on the web. What is the
Configuring TinyMCE to allow for tags, based on a customer requirement. My config is
Is it possible to replace javascript w/ HTML if JavaScript is not enabled on

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.