MATLAB Answers

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

12 views (last 30 days)
AAN on 8 Jan 2021
Commented: AAN on 8 Jan 2021
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 =;
% 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 =;

  1 Comment

Stephen Cobeldick
Stephen Cobeldick 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

Stephen Cobeldick
Stephen Cobeldick on 8 Jan 2021
Edited: Stephen Cobeldick 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)




Community Treasure Hunt

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

Start Hunting!