Got Questions? Get Answers.
Discover MakerZone

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn more

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today

Thread Subject:
Many structure fields as input arguments for function?

Subject: Many structure fields as input arguments for function?

From: Niels

Date: 12 Apr, 2013 10:17:08

Message: 1 of 2


I am in the process of converting a whole lot of code written with cells to code written with structures and I was wondering if something like the following could be written in a more efficient way. The given example is very basic and apart from its form in no way relevant to the actual code (my cells may also contain large matrices instead of single strings/scalars), but I think it should suffice to explain what I am trying to do...

Say, my original code would be like this:

A regular m-file named bodies.m
Density{1}=3.5e3; % [kg m-3]
Length{1}=2; % [m]
Width{1}=3; % [m]
Height{1}=4; % [m]
Density{25}=10.5e3; % [kg m-3]
Length{25}=2; % [m]
Width{25}=1.5; % [m]
Height{25}=0.5; % [m]

%% Perform calculations

for ii=1:length(Shape)

A function m-file named ShapeCalc.m
function [V,m] = ShapeCalc(shape,rho,l,w,h)

if strcmp(shape,'RectPrism')
elseif strcmp(shape,'Ellipsoid')


Now I am actually trying to do two things at once. I want to get rid of the for-loop and also of the many different cells, while at the same time keeping things clear.

The rewritten m-file named bodies.m with just the cells replaced by a structure.
Body(1)=struct('Shape','RectPrism', 'Density',3.5e3, 'Length',2, 'Width',3, 'Height',4);
Body(25)=struct('Shape','Ellipsoid', 'Density',10.5e3, 'Length',2, 'Width',1.5, 'Height',0.5);

%% Perform calculations

for ii=1:length(Body)

Now this example only has a few input arguments, but the line is already getting quite long - also compared to the cell-case. My thought was that since every necessary input variable is in the same structure already, there must be a cleaner way to do this input.

Furthermore, in an attempt to remove the for-loop, I got into a lot of trouble with the function m-file. I also tried it without a function, but I have (in both cases) no idea how to handle with the if-elseif statement from the original code. In this example I could split up the different shapes into different structures and then just do the following:

[BBody.Volume] = V{:};
[BBody.Mass] = m{:};

However, this is not an option for the actual case. Unfortunately...

Maybe for-loops are the only good way, but in that case I would really need a solution to the (to be) extremely large in- and output variables. If it's possible to do the entire thing in a more elegant way, I'd love to hear it!

Subject: Many structure fields as input arguments for function?

From: Niels

Date: 12 Apr, 2013 12:00:08

Message: 2 of 2

> %% Perform calculations
> for ii=1:length(Shape)
> [Volume{ii},Mass{i}]=ShapeCalc(Shape{i},Density{i},Length{i},Width{i},Height{i});
> end
> ------------------------------

I managed to simplify the input arguments of this part by converting the structure to cells:

CShape = struct2cell(Shape);
for ii=1:length(Shape)

However, I can't seem to do a similar thing for the output arguments. Rest of the problems have not been solved yet either.

Tags for this Thread

No tags are associated with this thread.

What are tags?

A tag is like a keyword or category label associated with each thread. Tags make it easier for you to find threads of interest.

Anyone can tag a thread. Tags are public and visible to everyone.

Contact us