Need help with reading few row of my data and saving it in a new txt file

I have 1*1 matrix. Its a big file of 6MB. I want to read few row and save those data in a new .txt file. I was trying this code
for i=0:1;
M=[num2str(1+i),'.txt'];
data1=dlmread(M);
t1=data1(0:80000);
fnamec=[num2str(1+i), 'xm.txt'];
dlmwrite(fnamec,t1);
end
getting error ??? Error using ==> dlmread at 145
Out of memory. Type HELP MEMORY for your options.
Error in ==> textread at 3
data1=dlmread(M);
Please help me with a code that will help me do the work Thanks for your time

1 commentaire

per isakson
per isakson le 9 Oct 2013
Modifié(e) : per isakson le 9 Oct 2013
"Error in ==> textread at 3 data1=dlmread(M)"
textread is a function of Matlab.

Connectez-vous pour commenter.

 Réponse acceptée

dpb
dpb le 9 Oct 2013
Modifié(e) : dpb le 9 Oct 2013
Probably simplest to copy a few lines from A to B is to just use fgets() --
SOTOO (NB: aircode...)
fid1=fopen('yourinputfile','r');
fid2=fopen('youroutputfile','w');
N=input('How many lines to copy?');
for idx=1:N
fprintf(fid2,'%s',fgets(fid1));
end
fclose('all')

4 commentaires

Now if I want to copy from line no 10 to 20, what command I need to use?
Thanks
dpb
dpb le 11 Oct 2013
Modifié(e) : dpb le 11 Oct 2013
Add a second input variable of first line to save and then either keep the number to save or change to ask for actual second line number, your choice.
In that case you then have one of at least a couple of ways to structure the code--
a) insert a second loop that before the existing that reads the number of lines to skip before the second writes the number to save, or
b) convert from counted loop to while ~feof() and keep counter for the two line locations.
The first is somewhat simpler w/ a little more code; the latter might be considered a little more "elegant" w/ the price of some logic...
doc continue
doc break
may be of use...
Give it a go and see; post back your attempts and any specific problems you encounter--it's a fairly good learning exercise for a couple of things.
I have tried the following according to your suggestion and worked, will try 2nd one also and update
fid1=fopen('x','r');
fid2=fopen('a','w');
fid3=fopen('b','w');
N=10;
for idx=1:N
fprintf(fid2,'%s',fgets(fid1));
end
M=20;
for idy=N:M
fprintf(fid3,'%s',fgets(fid1));
end
fclose('all');
The above writes two files which may be the intent; if otoh, the idea is to just not write the first N lines then
for idx=1:N
fgetl(fid1);
end
M=20;
for idx=1:M
fprintf(fid2,'%s',fgets(fid1));
end
Will do. Also NB in your solution that you're not actually doing M additional lines after the first N; you're doing M-N+1. If, for example, you asked to skip 10 and write the following 3 your second loop wouldn't execute at all... (exercise for student--why?)

Connectez-vous pour commenter.

Plus de réponses (0)

Community Treasure Hunt

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

Start Hunting!

Translated by