I have a .mat file which consist of a 22253519x5 double. I need to extract first three coloums only and save them in another .mat file having 2253519x3 double data. All the rows are to be considered.
I have no clue about this. New to MATLAB. Any help would be appreciated.

 Réponse acceptée

Walter Roberson
Walter Roberson le 29 Mai 2018

0 votes

The below would be easier if you only had one variable stored in the .mat file and you knew the name of the variable. The below code does not assume that there is only one variable and does not assume that the variables have any particular names; the code takes the first three columns of all of the variables that are present, and creates the new .mat file using all of the same variable names.
filename = 'YourFile.mat';
newfile = 'YourNewFile.mat';
datastruct = load(filename);
newstruct = structfun(@(M) M(:,1:3), datastruct, 'uniform', 0);
save(newfile, 'newstruct', '-struct');

6 commentaires

Moazza
Moazza le 29 Mai 2018
Thank you so much for your response . I am getting an error by running the code mentioned
Error using save Unknown command option.
Error in coloums_extract (line 6) save(newfile, 'newstruct', '-struct ');
Stephen23
Stephen23 le 29 Mai 2018
Modifié(e) : Stephen23 le 29 Mai 2018
The save option order is incorrect, '-struct' comes before the variable name:
save(newfile, '-struct','newstruct')
Moazza
Moazza le 30 Mai 2018
Thank you so much . it works for me.
Moazza
Moazza le 1 Juin 2018
newfile is not the one i required , it is saved as a c/h file, not containing 2253519x3 double data. please suggest .
What is a c/h file ?
>> some_variable = rand(22253519,5);
>> save('YourFile.mat', 'some_variable');
>> filename = 'YourFile.mat';
newfile = 'YourNewFile.mat';
datastruct = load(filename);
newstruct = structfun(@(M) M(:,1:3), datastruct, 'uniform', 0);
>> save(newfile,'-struct','newstruct')
>> whos -file YourNewFile.mat
Name Size Bytes Class Attributes
some_variable 22253519x3 534084456 double
Moazza
Moazza le 6 Juin 2018
thank you so much or your help :)

Connectez-vous pour commenter.

Plus de réponses (0)

Catégories

En savoir plus sur Vehicle Dynamics Blockset dans Centre d'aide et File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!

Translated by