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

  • SEARCH
  • Home
  • 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 9308219
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 19, 20262026-06-19T00:34:41+00:00 2026-06-19T00:34:41+00:00

I have a 1xn struct. The structs contains some fields with numeric cells. Not

  • 0

I have a 1xn struct. The structs contains some fields with numeric cells. Not every struct has the same fields. So I would like to add the missing fields to the struct. But I didn’t get it.

%  Mimimal example
%  I have same cells, imported from csv by csv2cell()

clear

dataCell{1}={"field_a","field_b","field_c"; ...
    1,2,3; ...
    4,5,6};

dataCell{2}={"field_a","field_b","field_d"; ...
    1,2,4; ...
    4,5,8};

%   Then I convert them to a struct
for ii=1:numel(dataCell)    
DataStruct{ii}=cell2struct((dataCell{ii}(2:end,:)),[dataCell{ii}(1,:)],2);
  try fields=[fields, fieldnames(DataStruct{ii})']; % fails for the first loop, because 'fields' doesn't exist yet
  catch
   fields=[fieldnames(DataStruct{ii})']; % create 'fields' in the first loop
  end
end

% Create a list of all existing fields
fields=unique(fields);

% Now look for the missing fields and add them to the struct
for jj=1:numel(DataStruct)
  missing_fields{jj} = setdiff(fields,fieldnames(DataStruct{jj}));
  for kk=1:numel(missing_fields)
    DataStruct{jj}.(missing_fields{jj}{kk})=NaN*zeros(numel(dataCell{jj}(2:end,1)),1); % Execution fails here!
  end
end

This leads to an error:

Octave Error

error: invalid assignment to cs-list outside multiple assignment
error: called from:
error:   ~/example.m at line 29, column 44

Matlab Error

Insufficient outputs from right hand side to satisfy comma separated
list expansion on left hand side.  Missing [] are the most likely cause.

The problem is, that the output of DataStruct{1}.field_a is not a matrix or a cell, but multiple ans. Is there a easy possibility to convert the cells to a matrix or a better possibility to import the csv?

the other good possibility would be something like this

 DataStruct{jj}=setfields(missing_fields{jj},NaN_Values{jj})

where missing_fields{jj} and NaN_Values are both cells with the same length, so that i can set multiple fields at once.

  • 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-19T00:34:43+00:00Added an answer on June 19, 2026 at 12:34 am

    You use the deal function to distribute output to inputs. For your example:

    % Now look for the missing fields and add them to the struct
    for jj=1:numel(DataStruct)
      missing_fields{jj} = setdiff(fields,fieldnames(DataStruct{jj}));
      for kk=1:numel(missing_fields)
        [DataStruct{jj}.(missing_fields{jj}{kk})] = deal(NaN*zeros(numel(dataCell{jj}(2:end,1)),1));
      end
    end
    

    Note that you will get an Index exceeds matrix dimensions, but this is due to a different problem – I’m sure you’ll be able to sort that one out!

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

Sidebar

Related Questions

I have a n-dimensional vector (1xn dataset, and it is not image data), and
Have a look at the menu link Produkter on http://marckmann.se/ I would like to
Have a numeric nvarchar values with some extra-symbols For example 1/2, 9/6, 14A They
have written this little class, which generates a UUID every time an object of
Have a procedure which looks like Procedure TestProc(TVar1, TVar2 : variant); Begin TVar1 :=
Have deployed numerous report parts which reference the same view however one of them
Here is my question : I have A = 1xN matrix and B =
So, I have a cell-array of 1xN vectors of different lengths. I want to
have numerous tests working, but my actual test of some objects is failing and
I would like to plot connected points in MATLAB. My connected points come from

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.