Efficient methods for MATLAB code

조회 수: 2 (최근 30일)
Julian
Julian 2018년 8월 30일
편집: Matt J 2018년 8월 30일
Hi everyone,
i'm quite new to MATLAB and also programming as such.
I'm currently refactoring an old Software. Often when i'm creating new functions i need a lot of variables as Input. I guess some of them could be stored in the handles structure that is constantly being passed around.
So now i'm wondering what is more efficient, respectivley less time comsuming a) Storing variables in the handle structure(making it larger and maybe heavier to handle for MATLAB) b) Creating functions that have a lot of Input variables
I'm just curious and it would be great if someone could answer this.
Best regards Julian

채택된 답변

Matt J
Matt J 2018년 8월 30일
Both are equivalent as far as consuming computational resources is concerned. There is no fundamental difference between structure fields and individual variables. From Matlab's point of view, it's just a different way of naming them.
  댓글 수: 2
Julian
Julian 2018년 8월 30일
편집: Julian 2018년 8월 30일
Thanks for your answer. This is good to know.
But is MATLAB also indifferent about a handle structure with say 240 components and a handle structure with 300 components?
Because my handles get passed around a lot between functions and if a bigger handle structure slows down the whole programm, i would be more careful with extanding the handle structure. So say i have the possibility to make a function with an Input of one variable, but i have also stored this variable in my handle structure. Does it make more sense to use the handle or to assign the individual variable in terms of speed. Do you know what i mean?
Matt J
Matt J 2018년 8월 30일
편집: Matt J 2018년 8월 30일
If you have lots of related variables that often need to travel together, it makes sense to store them in a structure and pass them around that way. That's what structures are for.
In terms of speed and computational burden, there is no big difference between passing 1 variable or a million variables. Matlab isn't making copies of these variables every time you pass them to a function, if that's what you're worried about. They all get passed with pointers. See, for example,

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Variables에 대해 자세히 알아보기

제품


릴리스

R2018a

Community Treasure Hunt

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

Start Hunting!

Translated by