How to load multiple specified variables in a specified mat file?
21 ビュー (過去 30 日間)
古いコメントを表示
For example, I want load Train_data Train_label Test_data Test_label in each all_number.mat, such as in figure.
But there have bugs in 'file'. How to debug?
for iter_all = 1:20
file = ['all',num2str(iter_all),'.mat'];
load('file' Train_data Test_data Train_label Test_label)
end
0 件のコメント
回答 (1 件)
Stephen23
2020 年 1 月 7 日
編集済み: Stephen23
2020 年 1 月 7 日
Your code does not make much sense: you added single quotes around the variable name (making it a literal string), you removed the single quotes from the fieldnames (making them attempts to access variables or call functions), and you did not use any commas to separate the input arguments. You should read the load documentation and look at its examples.
Something like this should get you started (the data will be in cell array C:
N = 20;
C = cell(1,N); % preallocate
T = {'Train_data','Test_data','Train_label','Test_label'};
for k = 1:N
F = sprintf('all%d.mat',k);
C{k} = load(F,T{:});
end
I recommend concatenating the scalar structures into one non-scalar structure:
S = [C{:}]; % concatenate all loaded data into one structure
which you can access using indexing, e.g. the first file:
S(1).Train_data
S(1).Test_data
4 件のコメント
Stephen23
2020 年 1 月 7 日
"But I dont need to load all data at once."
I don't see the relevance. Loading into an output structure will make your code more robust.
Consider what your code would do if one of the files does not contain one of those variables, or if it contains a variable with the same name as one that you use in your code... you would get some very unpredictable behaviors.... simple to avoid by just loading into an output variable.
参考
カテゴリ
Help Center および File Exchange で Workspace Variables and MAT-Files についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!