How to properly extract dataset and load in new file? Keep getting error.

2 views (last 30 days)
I have original dataset from which I have extract subset
% Load original data variable: 100 x 100 x 1000 x 500
folder = 'C:\Users\ABC\Desktop\Raw';
dname = "D";
X = load(strcat(folder,'\',dname,'_X.mat'));
X = X.data();
% Extract from variable 10 x 10 x 10 x 10 subset and save
Xs = X(1:10;1:10;1:10;1:10);
save ('D_Xs.mat', Xs)
Now when I open a new file and try to load in the extracted subset I get an error stating no data.
% Loading Ex
folder = 'C:\Users\ABC\Desktop\Raw';
dname = "D";
X = load(strcat(folder,'\',dname,'_Xs.mat'));
X = X.data();
  1 Comment
Stephen23
Stephen23 on 8 Jan 2021
You data importing uses absolute paths (which is good), but your data exporting does not (which is bad):
save(fullfile(folder,'D_Xs.mat'), Xs)
Using fullfile is recommended over string concatenation.

Sign in to comment.

Accepted Answer

Stephen23
Stephen23 on 8 Jan 2021
Edited: Stephen23 on 8 Jan 2021
There is no data field because you saved the array using the name Xs. So you need to use the field Xs (and get rid of the superfluous parentheses):
tmp = load(strcat(folder,'\',dname,'_Xs.mat'));
X = tmp.Xs;
% ^^ use the correct field name
If you want the variable in the new mat file to be named data, then you need to name it data (and not Xs).

More Answers (0)

Categories

Find more on Data Import and Analysis in Help Center and File Exchange

Products


Release

R2020a

Community Treasure Hunt

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

Start Hunting!