Discover MakerZone

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn more

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today

Thread Subject:
save data to mat file

Subject: save data to mat file

From: edward kabanyas

Date: 12 Apr, 2012 00:07:28

Message: 1 of 5

Hi all,

I am reading many data files and hope to save the structured data from those files to a mat file. I did as follow:

clear all;close all

%Making file list

fileList = getAllFiles( '*.txt');
[n, m] = size(fileList);
for jj=1:n
    
    %..data processing until I got an structure datpr (591 x 1 struct) for
    %the first file for example, then I save as:
    
    save('newstruct.mat', 'datpr' 'append');
        
end

Problem, I got only datpr for the last file and those of previous files are deleted.
How to save the data to an existing mat file without deleting the existing data ?

Thanks for help

Edward

Subject: save data to mat file

From: TideMan

Date: 12 Apr, 2012 03:02:29

Message: 2 of 5

On Thursday, April 12, 2012 12:07:28 PM UTC+12, edward kabanyas wrote:
> Hi all,
>
> I am reading many data files and hope to save the structured data from those files to a mat file. I did as follow:
>
> clear all;close all
>
> %Making file list
>
> fileList = getAllFiles( '*.txt');
> [n, m] = size(fileList);
> for jj=1:n
>
> %..data processing until I got an structure datpr (591 x 1 struct) for
> %the first file for example, then I save as:
>
> save('newstruct.mat', 'datpr' 'append');
>
> end
>
> Problem, I got only datpr for the last file and those of previous files are deleted.
> How to save the data to an existing mat file without deleting the existing data ?
>
> Thanks for help
>
> Edward

Nest the structure (e.g., s(jj).datpr) and save outside the loop, or put each structure in a new .mat file.
When you append, Matlab reads in the whole file, adds your variable to it and writes out the combined file. If your variable has the same name as the variable already saved, it gets overwritten.

Subject: save data to mat file

From: edward kabanyas

Date: 12 Apr, 2012 05:26:24

Message: 3 of 5

TideMan;

Thanks for reply

> Nest the structure (e.g., s(jj).datpr) and save outside the loop, or put each structure in a >new .mat file.

Now in my mind, I want to name the structured data for each input file by using different name. Therefore, I can save easily all structure from all data to a file. Again, thank you..

Edward




 <mulgor@gmail.com> wrote in message <3612506.34.1334199749318.JavaMail.geo-discussion-forums@vbuf25>...
> On Thursday, April 12, 2012 12:07:28 PM UTC+12, edward kabanyas wrote:
> > Hi all,
> >
> > I am reading many data files and hope to save the structured data from those files to a mat file. I did as follow:
> >
> > clear all;close all
> >
> > %Making file list
> >
> > fileList = getAllFiles( '*.txt');
> > [n, m] = size(fileList);
> > for jj=1:n
> >
> > %..data processing until I got an structure datpr (591 x 1 struct) for
> > %the first file for example, then I save as:
> >
> > save('newstruct.mat', 'datpr' 'append');
> >
> > end
> >
> > Problem, I got only datpr for the last file and those of previous files are deleted.
> > How to save the data to an existing mat file without deleting the existing data ?
> >
> > Thanks for help
> >
> > Edward
>
> Nest the structure (e.g., s(jj).datpr) and save outside the loop, or put each structure in a new .mat file.
> When you append, Matlab reads in the whole file, adds your variable to it and writes out the combined file. If your variable has the same name as the variable already saved, it gets overwritten.

Subject: save data to mat file

From: edward kabanyas

Date: 12 Apr, 2012 06:48:13

Message: 4 of 5

Hi TideMan,

It seems difficult to make the structure name changeable for each input file, hopefully you have suggestion. I tried as:

[n, m] = size(fileList11);
for jj=1:n
  FILE_NAME=fileList11{jj}
  %data process..
outfile1 = sprintf('%s%s',FILE_NAME(12:end-30), '.mat');
name =sprintf('%s%s',FILE_NAME(12:end-30));
name = struct('time',[year month hour day],'nest',stats);

if jj==1;
save(outfile1, 'name');
else
    save(outfile1, 'name', '-append');
end
end

with the above code, the structure is "name", not the name =sprintf('%s%s',FILE_NAME(12:end-30)); How to make it changeable ? Therefore, I can save each structure with different name. Thank you...

Edward



 <mulgor@gmail.com> wrote in message <3612506.34.1334199749318.JavaMail.geo-discussion-forums@vbuf25>...
> On Thursday, April 12, 2012 12:07:28 PM UTC+12, edward kabanyas wrote:
> > Hi all,
> >
> > I am reading many data files and hope to save the structured data from those files to a mat file. I did as follow:
> >
> > clear all;close all
> >
> > %Making file list
> >
> > fileList = getAllFiles( '*.txt');
> > [n, m] = size(fileList);
> > for jj=1:n
> >
> > %..data processing until I got an structure datpr (591 x 1 struct) for
> > %the first file for example, then I save as:
> >
> > save('newstruct.mat', 'datpr' 'append');
> >
> > end
> >
> > Problem, I got only datpr for the last file and those of previous files are deleted.
> > How to save the data to an existing mat file without deleting the existing data ?
> >
> > Thanks for help
> >
> > Edward
>
> Nest the structure (e.g., s(jj).datpr) and save outside the loop, or put each structure in a new .mat file.
> When you append, Matlab reads in the whole file, adds your variable to it and writes out the combined file. If your variable has the same name as the variable already saved, it gets overwritten.

Subject: save data to mat file

From: edward kabanyas

Date: 13 Apr, 2012 14:28:16

Message: 5 of 5

Dear TideMan,

> Nest the structure (e.g., s(jj).datpr)

I have tried it.

clear all;close all

%Making file list

fileList = getAllFiles( '*.txt');
[n, m] = size(fileList);
for jj=1:n
    
    %..data processing until I got an structure datpr (591 x 1 struct) for
    %the first file for example, then I save as:
    
    test= struct('time',[year month hour day],'nest',datpr);
    s(jj).test =test;
        
end

However, when I open s structure in the end of looping, test is empty. Could you show me how to nest the test, to get an accumulated test at the end of looping and then save all to a mat file. Thanks for help..

Edward



<mulgor@gmail.com> wrote in message <3612506.34.1334199749318.JavaMail.geo-discussion-forums@vbuf25>...
> On Thursday, April 12, 2012 12:07:28 PM UTC+12, edward kabanyas wrote:
> > Hi all,
> >
> > I am reading many data files and hope to save the structured data from those files to a mat file. I did as follow:
> >
> > clear all;close all
> >
> > %Making file list
> >
> > fileList = getAllFiles( '*.txt');
> > [n, m] = size(fileList);
> > for jj=1:n
> >
> > %..data processing until I got an structure datpr (591 x 1 struct) for
> > %the first file for example, then I save as:
> >
> > save('newstruct.mat', 'datpr' 'append');
> >
> > end
> >
> > Problem, I got only datpr for the last file and those of previous files are deleted.
> > How to save the data to an existing mat file without deleting the existing data ?
> >
> > Thanks for help
> >
> > Edward
>
> Nest the structure (e.g., s(jj).datpr) and save outside the loop, or put each structure in a new .mat file.
> When you append, Matlab reads in the whole file, adds your variable to it and writes out the combined file. If your variable has the same name as the variable already saved, it gets overwritten.

Tags for this Thread

What are tags?

A tag is like a keyword or category label associated with each thread. Tags make it easier for you to find threads of interest.

Anyone can tag a thread. Tags are public and visible to everyone.

Contact us