How to create a very large array with an incremently named variable?

30 ビュー (過去 30 日間)
Scotty Mac
Scotty Mac 2025 年 11 月 4 日 22:57
コメント済み: Stephen23 2025 年 11 月 4 日 23:24
I searched and searched and can't find the answer to this problem...
How can I create a large array from entered inputs without brute forcing it?
I have:
timing_array=[timing1, timing2, timing3, timing4, timing5, timing6, timing7, timing8, timing9, timing10];
Is there an easier way to do this, if I have, say timing values from 1 to 512 (timing1, timing2, ....timing512)?
Thanks in advance!
  2 件のコメント
John D'Errico
John D'Errico 2025 年 11 月 4 日 23:00
A huge reason why it was a terrible idea in the first place to number your variables like that. LEARN TO USE ARRAYS!
Stephen23
Stephen23 2025 年 11 月 4 日 23:24
"Is there an easier way to do this"
Yes: use better data design that does not force meta-data (e.g. pseudo-indices) into variable names:
You forgot to tell us the most important information of all: how did you get all of those variables into the workspace? That is one place where you could fix this data design, for example by LOADing into an output variable rather than directly into the workspace:
S = load(..);
Even better data design would use actual indexing, not pseudo-indices.

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

採用された回答

Walter Roberson
Walter Roberson 2025 年 11 月 4 日 23:12
Don't Do That.
If you are loading arrays from a single .mat file, then instead of using
load FILENAME
use
STRUCTURE = load(FILENAME);
timing_array = struct2array(orderfields(STRUCTURE));

その他の回答 (0 件)

カテゴリ

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

タグ

製品


リリース

R2019a

Community Treasure Hunt

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

Start Hunting!

Translated by