How can I save structures from a cellarray into individual .mat files with a loop?
3 visualizaciones (últimos 30 días)
Mostrar comentarios más antiguos
Stebe Hauser
el 20 de Mayo de 2019
Comentada: Stebe Hauser
el 21 de Mayo de 2019
I want to be able to change a whole folder of a dataset all at once in the same way, just with an extension of the given names of the files and save it in a new folder. To read and process the data I don't have any Problem. But when I'm using the code as below (4.), each generated file contains all the informations. e.g. A contains only the infromations of A, but A_cut contains the processed information of the prepared A and B.
Below you find my code:
- Is for generating what my data looks like.
- Is How I read all of the folders in one cell array without loosing any information
- is to prepare them
- is where I don't know what the Problem is.
clear all
% 1.Create data (according to the data i'm actually using)
A.spc=rand(100,1);
A.wavenumber=[1:100].';
A.header={'first','second','third'}.';
B.spc=rand(100,1);
B.wavenumber=[1:100].';
B.header={'first','second','third'}.';
%save the data as file
save('A.mat','A')
save('B.mat','B')
%% 2.read data
clear all
clc
location=uigetdir;
info=dir([location,'\*.mat']);
filenames={info(:).name};
data=cell(length(info),1);
for i=1:length(info)
fullname=[location,'\',info(i).name];
data{i}=importdata(fullname,'\t',14,0);
end
%% 3.data procession
datacut=data; %for header information
for j = 1:length(data)
datacut{j,1}.wavenumber = data{j,1}.wavenumber(j, :);
datacut{j,1}.spc = data{j,1}.spc(j, :);
end
%% 4.save data
savepath=uigetdir(cd,'Save to folder');
str_affix = inputdlg('Enter string affix:','String Affix',[1 50],{'cut'});
for i = 1:length(data)
c_string=string(strcat(savepath,'\',erase(info(i).name,'.mat'),'_',str_affix,'.mat'));
save(c_string, 'datacut');
end
3 comentarios
Stebe Hauser
el 21 de Mayo de 2019
Editada: Stebe Hauser
el 21 de Mayo de 2019
Respuesta aceptada
Bob Thompson
el 20 de Mayo de 2019
It is saving all of the data because you are not indexing your save selection.
save(c_string,'datacut');
You will need to create a temporary variable which contains just the desired cell, as you cannot index in the save command directly.
tmp = datacut{:};
save(c_string,'tmp')
3 comentarios
Stephen23
el 21 de Mayo de 2019
Editada: Stephen23
el 21 de Mayo de 2019
"... but when you compare A_cut.mat with B_cut.mat there are the same values."
Sure, because within the loop the data that you refer to is exactly the same on each loop iteration. In fact you told MATLAB to take the content of the first cell of datacut:
tmp = datacut{:};
% ^^^ comma separated list.
%^^ first cell's content.
You will need to use indexing to access a different cell's data on each loop iteration, e.g.:
tmp = datacut{i};
% ^ loop index
Más respuestas (0)
Ver también
Categorías
Más información sobre Data Type Conversion en Help Center y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!