How to import multiple .mat files to workspace?
3 ビュー (過去 30 日間)
古いコメントを表示
Hi,
As cited above, I'm trying to import multiple .mat files (total 72 no. of .mat files) which are at the location as in the code below. However the code doesn't import the files to the workspace and throws error as below.
files = dir('D:\Rahul\Data_tokamak\data&plot\data_paper1\H-mode data\H-mode bistable model\set1_neoAno_ratio_10\beta0.01\Contourplot\*.mat');
for i=1:length(files)
load(files(i).name,'-ascii');
end
ERROR:
Error using load
with regards,
rc
0 件のコメント
採用された回答
Stephen23
2024 年 4 月 28 日
編集済み: Stephen23
2024 年 4 月 30 日
The basic problem is that you are not supplying the filepath to LOAD.
You can improve your code as well. I am guessing that you are importing text files of numeric data:
P = 'D:\Rahul\Data_tokamak\data&plot\data_paper1\H-mode data\H-mode bistable model\set1_neoAno_ratio_10\beta0.01\Contourplot';
S = dir(fullfile(P,'*.mat'));
for k = 1:numel(S)
F = fullfile(S(k).folder,S(k).name);
M = readmatrix(F); % better function for importing numeric data from text files
S(k).data = M;
end
All of the imported data is stored in the structure S. For example, the 2nd file:
S(2).name % filename
S(2).data % imported file data
Depending on the content of the MAT files you may be able to concatenate them into one structure array:
A = cat(3,S.data) % optional, all imported data in one array
Note that this approach is much better than LOADing directly into the workspace as you showed in your question.
3 件のコメント
Stephen23
2024 年 4 月 30 日
編集済み: Stephen23
2024 年 5 月 2 日
"it gives error unable to recognise the data files as below: ERROR: Unrecognized function or variable 'H0_27bi_001'."
In your original code you LOADed all of the data directly into the workspace, apparently giving you lots and lots of numbered variables in the workspace. That is not good data design and should be avoided.
The approach I showed you uses arrays and indexing, which is the recommended approach in MATLAB (rather than having lots and lots of separate variables in the workspace). Of course this also means that you will need to modify the rest of your code to suit, e.g. by referrring to one array instead of lots and lots and lots of separate variables in the workspace (ugh).
That is why I showed you the example of one way to concatenate the data together into one array, which I told you in my answer: "All of the imported data is stored in the structure S."
There is no single solution for processing data which works for all data: it depends entirely on the classes and sizes of your data, which is information you have not told us. So I cannot guess which approach you need, because you have not given us this information. Therefore you will either have to:
- modify your code yourself, or
- upload some sample data files by clicking the paperclip button.
Making me guess what data you have is not an efficient way to debug code that you have not shown me.
But because apparently you want me to use my magic crystal ball, lets try this:
P = 'D:\Rahul\Data_tokamak\data&plot\data_paper1\H-mode data\H-mode bistable model\set1_neoAno_ratio_10\beta0.01\Contourplot';
S = dir(fullfile(P,'*.mat'));
for k = 1:numel(S)
F = fullfile(S(k).folder,S(k).name);
M = readmatrix(F);
S(k).data = M(2,1);
end
V = [S.data]
and then you have the (2,1) data in vector V.
その他の回答 (1 件)
Paul
2024 年 4 月 28 日
Why use the -ascii option to load a .mat file? If that's not the problem, please edit your question by copy/pasting the entire error message.
Depending on what's in the .mat files and how you're planning to process the data, you might want to consider using the output argument of load so that the data from each .mat file is stored in an element of an indexable array.
0 件のコメント
参考
カテゴリ
Help Center および File Exchange で Standard File Formats についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!