Saving each data from for loop
Show older comments
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
Answers (1)
Philippe Lebel
on 25 Nov 2019
Edited: Philippe Lebel
on 25 Nov 2019
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 Comments
Stephen23
on 25 Nov 2019
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
on 25 Nov 2019
Ho thanks for letting me know, modified my solution.
helia mb
on 25 Nov 2019
Stephen23
on 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
on 28 Nov 2019
Stephen23
on 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
on 29 Nov 2019
"...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
on 29 Nov 2019
Stephen23
on 29 Nov 2019
"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
on 29 Nov 2019
Categories
Find more on Logical in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!