Using load for different files with varying names
3 ビュー (過去 30 日間)
I have 25 files with .mat files, naming from data1 to data25
how to load them properly without manualy typing from 1 to 25
Here is my idea and the error
for i = 1:1:25
Error using load
Must be a text scalar.
その他の回答 (1 件)
Stephen23 2022 年 9 月 14 日
編集済み: Stephen23 2022 年 9 月 14 日
Here is a robust approach, by not LOADing directly into the workspace, which also also resolves your follow-up question. The code assumes exactly one variable per MAT file.
N = 25;
C = cell(1,N);
for k = 1:N
F = sprintf('data%d.mat',k);
C(k) = struct2cell(load(F));
All of your imported filedata wll be in the cell array C. Note that you can trivially loop over all of C, or access its contents individually. For example, the data for the second file:
When you learn to avoid having meta-data in variable names (e.g. pseudo-indices), then you can start to learn how to write neat, simple, and very efficient MATLAB code.