Saving each data from for loop

Hello, I have a for loop that count from 1 to 200 . And these 200 are number of my file that gonna read in matlab. I want to read each file and save it.but lenght of my files aren't same as each other.how can I save each of them?! Wgen I running my for loop it just save the last data.
if true
For i =1:200
Clc
Close all
Ss2 %my matrix are in there
Wanna save each of them
End
end

2 Kommentare

Michal
Michal am 25 Nov. 2019
Hi, by 'how can I save each of them?!' do you mean saving the contents of the files in one variable? What are the contents of the files?
helia mb
helia mb am 25 Nov. 2019
Yes,they are series of numbers about 38×360000, but the column in each file are varied. When i load my for loop , it reads each of them but at the end i can see just the last matrix, that means other doesn't save in workspaces

Melden Sie sich an, um zu kommentieren.

Antworten (1)

Philippe Lebel
Philippe Lebel am 25 Nov. 2019
Bearbeitet: Philippe Lebel am 25 Nov. 2019

0 Stimmen

If you want to store elements (like strings) that are not the same size you can use cells.
A = {'abc', 'defg', 'hijklmnop', 123, [4,5,6,7,8], false}
A =
'abc' 'defg' 'hijklmnop' [123] [1x5 double] [0]
So if you want to go through a bunch of files and put their content in a cell you can do it this way (pseudo code)
my_list_of_files= {'file1','file2', 'file3'}
my_array = {};
for i=1:length(my_list_of_files)
my_array{i} = read(my_list_of_files{i});
end

12 Kommentare

Note that square brackets are a concatentation operator, so this code:
['file1','file2', 'file3']
is exactly equivalent to this:
'file1file2file3'
Perhaps you intended to use a cell array of char vectors, or a string array?
Philippe Lebel
Philippe Lebel am 25 Nov. 2019
Ho thanks for letting me know, modified my solution.
helia mb
helia mb am 25 Nov. 2019
Thank you for answering me so well. But there are not string. I have 200 files, that every time in the loop one of them would be read , they are all numbers 38×36000.But the coulmn size are not same as each other.I want to read each of them while reading them saving them too. Cause when i run my code it just show the last one and others disapperad.
Stephen23
Stephen23 am 26 Nov. 2019
"Cause when i run my code it just show the last one and others disapperad."
You need to use indexing to store the imported data in an array. Using a cell array would be a good start, as the MATLAB documentation shows:
helia mb
helia mb am 28 Nov. 2019
Hi, Thank u again.but i don't wanna import or export my files. My data type are bdf files and they are about 200 trial. So if I want to read them to extract data, I should use bdf-reader. Bdf-reader has some codes and for using them i should mentioned my files name. So I used "FOR" loop for reading each data and preprocessing them and save them. But the lenght of matrix are not same az each other. All of them has 38 Row and about 36000 column(diffrent in each trial). I want to save these 200 data. I can't used that site refrences cause I can't read bdf files by matlab and save them. These files at first step should be opened. I really appreciate if u could help me to figure this out. Thank u again.
Stephen23
Stephen23 am 29 Nov. 2019
"but i don't wanna import or export my files"
According to your original question you do. You wrote "I want to read each file and save it.", where you wrote "read" corresponds to "import" and "save" corresponds to "export". According to standard MATLAB terminology:
  • "import" = read data from a file into MATLAB memory.
  • "export" = save data from MATLAB memory into a file.
If you actually meant something totally different by "load" and "save" then you will need to explain it very carefully. Otherwise the link I gave you earlier tells you the basics of what you need to import ("read") and export ("save") data to and from files using MATLAB:
helia mb
helia mb am 29 Nov. 2019
No , as i said clearly they are not ordinary file like text or jpg, I should used function to open this files and export my data matrix, that's why I can't used easy way import and export and that's link u are mentioned earlier.I used 'for' loop to call each file and then used this function and extract my data matrix. The problem is my for loop extract and read every 200 data but show only the last one on my workspace.
Stephen23
Stephen23 am 29 Nov. 2019
Bearbeitet: Stephen23 am 29 Nov. 2019
@helia mb: please upload your file importing function by clicking the paperclip button.
"...as i said clearly they are not ordinary file like text or jpg, "
Regardless of the file format, importing/exporting sequences of files uses the same basic concepts (including using indexing to store the imported data in an array, e.g. a cell array). You can use whatever function you want to import that data, as long as you use indexing to store that data in an array.
"...but show only the last one on my workspace."
Did you allocate the imported data to an array, e.g. a cell array, using indexing? (Note that Philippe Lebel's answer showed you how to use a cell array with indexing to store the imported file data, as does the MATLAB documentation, so you should not have any difficulties doing this.)
Please show us the code that your most recent code attempt.
helia mb
helia mb am 29 Nov. 2019
<<
>>
helia mb
helia mb am 29 Nov. 2019
Bearbeitet: helia mb am 29 Nov. 2019
As u can see my files and bdf function , I don't know how to insert them in a cell array plus my column data size aren't same as each other.during reading each file I want to do some processing too and save it altoghether
"I don't know how to insert them in a cell array..."
N = number of files
C = cell(1,N);
for k = 1:N
... import your data
YourData = ... whatever
C{k} = YourData; % store your data in a cell array
end
"...plus my column data size aren't same as each other.during reading each file"
Is irrelevant if you are using a cell array.
helia mb
helia mb am 29 Nov. 2019
Yes exactly that's what I mean...oh my god Thank you so much~~ you are amazing

Melden Sie sich an, um zu kommentieren.

Gefragt:

am 25 Nov. 2019

Bearbeitet:

am 29 Nov. 2019

Community Treasure Hunt

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

Start Hunting!

Translated by