How can I break down a data matrix of say 1024x961 to 961 single files of 1024x1 matrix with filenames corresponding to the column number?

No programming experience before, generally use matlab for plotting figures. ask for several lines of code that can help me break down this data file into pieces (save them as -mat, -txt, or -ascii) so that I could proceed processing the data. Any help will be much appreciated!
e.g. spectrum_001=originalfile(:,1); spectrum_961=originalfile(:,961)

 Accepted Answer

a=rand(1024,961) % Example
for k=1:size(a,2)
m=a(:,k);
save(sprintf('file%d',k),'m') % save to mat file
end
But I don't see what is the aim of doing this?

8 Comments

azzi, thanks for the speedy reply. i tried your code, it's not what I want. I guess I didnt state my problem very clearly.
so i had this file a (1024x961), i want to break it down to 961 files named a_1, ..., a_961; each file is a column of the original file a. because the data processing software can only recognize 1024x1 matrix files. so i need to break them down to 961 files of single columns.
Ok. Read your file
a=dlmread('yourfile.txt')
for k=1:size(a,2)
m=a(:,k);
save(sprintf('a_%d',k),'m') % save to mat file
end
Thanks, Azzi. It worked. Really appreciate your help here.
Hi Azzi, is there a way to save these final single files as .txt, not as .mat? I tried multiple variations but still cannot achieve it.
e.g. i tried
for k=1:size(a,2)
m=a(:,k);
save(sprintf('a_%d.txt',k),'m') % save to txt file
end
the result is a bunch of non-recognizable files. I checked out the matlab help, didn't seem to help. I could save it as ascii though, using:
for k=1:size(F1,2)
m(:,1)=wavenumb_cor510;
m(:,2)=F1(:,k);
save(sprintf('OvCa_1_cell1_%s',num2str(k,'%3i')),'m','-ascii') % save to txt file
end
Why can't you just do fopen(), fprintf(), and fclose()? See their help for examples.
a=dlmread('yourfile.txt')
for k=1:size(a,2)
m=a(:,k);
file=sprintf('a_%d',k)
dlmwrite(file,m)
end

Sign in to comment.

More Answers (1)

Hao: Azzi's code should have done it. Perhaps you just need to have it use the same names you picked and to be a little more explicit in the steps, like I show in the code below:
filebrowser; % Make sure Current Folder panel is showing.
% Create sample data for this demo
originalfile = rand(1024,3);
% Define the folder where you want to save all the files.
folder = pwd; % Whatever folder you want
% Loop across all the columns, saving each one to its own file.
for column = 1 : size(originalfile, 2)
thisColumn = originalfile(:, column);
baseFileName = sprintf('spectrum_%3.3d.mat', column);
fullFileName = fullfile(folder, baseFileName);
save(fullFileName, 'thisColumn') % save to mat file
end

1 Comment

yes, Azzi's code worked. I didn't make it work previously because I didn't notice the files had already been created in the directory. I was simply looking at the workspace. Thank you as well.

Sign in to comment.

Community Treasure Hunt

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

Start Hunting!