Function that outputs a varying number of variables depending on input --> N inputs = N output variables

14 ビュー (過去 30 日間)
I have read that dynamic variables are generally bad practice and can be avoided using other methods. However, I was curious about a potential use case. I would like to make a function that takes in N inputs that correspond to the locations and filenames of several datasets and then the function loads the data and saves it to a variable (N inputs = N ouput variables). Is there a good way to do this? To my understanding I would be unable to use alternate methods given that the data I am loading are mutlidimensional arrays of varying dimensions.
I considered padding each array to the same size with NaNs, but I am very hesistant to use this method. Does anyone have any recommendations?
I only want to do this to save myself time with either copying and pasting or typing out the code to load each file separately.
Would using a struct be possible, where each field corresponds to a dataset?

採用された回答

Stephen23
Stephen23 2022 年 10 月 3 日
移動済み: Matt J 2022 年 10 月 3 日
"To my understanding I would be unable to use alternate methods given that the data I am loading are mutlidimensional arrays of varying dimensions."
Why does that stop you from using container arrays? That is precisely what container arrays are for !
"Is there a good way to do this?"
There are certainly better ways of doing this, some obvious approaches to try would be:
  • a non-scalar structure with fields e.g. LOCATIONS and FILENAMES.
  • one or two cell arrays, possibly in conjunction with VARARGIN and VARARGOUT.
See also:
  1 件のコメント
AES
AES 2022 年 10 月 3 日
移動済み: Matt J 2022 年 10 月 3 日
Thank you for the reply! This was very helpful and answers my question.

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeVariables についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by