How to read a file and write to a file?

The input .txt file has 1000s of rows like:
.......................
.......................
43218680 102.485 1.1488100498592582e+07 10
43218700 102.201 1.1488148531592581e+07 12
43218740 102.536 1.1488244644592581e+07 12
43218760 102.615 1.1488292688592581e+07 10
43218720 102.289 1.1488196602592580e+07 10
......................
......................
I need help writing a MATLAB code that would read from the file to write to multiple .txt files for every unique value in the last column (here 10 and 12, known beforehand). In this case there need to be two .txt output files. The first .txt file will have
.........................................
43218680 102.485 1.1488100498592582e+07 10
43218760 102.615 1.1488292688592581e+07 10
43218720 102.289 1.1488196602592580e+07 10
..............................................
The second output .txt file should have the rows
..........................................
43218700 102.201 1.1488148531592581e+07 12
43218740 102.536 1.1488244644592581e+07 12
.............................................
An equivalent C code that worked is
....................................
#include<stdio.h>
#include<stdlib.h>
main()
{ int m,s;
float p;
double c;
FILE *fp,*fp1,*fp2;
fp=fopen("3Mar.txt","r");
fp1=fopen("10.txt","w");
fp2=fopen("12.txt","w");
/* 565216000 95.611 4.9407952477976400e+06 24 */
while(fscanf(fp,"%d %f %lf %d",&m,&p,&c,&s)!=EOF)
{
if(s==10)
{
fprintf(fp1,"%d %f %lf \n",m,p,c);
}
if(s==12)
{
fprintf(fp2,"%d %f %lf \n",m,p,c);
}
}
}
........................
Please help. A sample input file is attached.

1 Comment

hi,
use can use file handling functions like fopen(), fprintf(), fscanf()

Sign in to comment.

 Accepted Answer

data = importdata('data.txt') ;
% last column
c = data(:,end) ;
% get repeated numbers
[rc,ia,ib] = unique(c) ;
for i = 1:length(rc) ;
k = c(c==rc(i)) ;
fname = strcat(num2str(rc(i)),'.txt') ;
save(fname,'k','-ascii') ;
end

24 Comments

Ironmaniac
Ironmaniac on 28 Nov 2016
Edited: Ironmaniac on 28 Nov 2016
Hi KSSV
Thanks for your code. But I am getting am error as in the screenshot. Could you help?
Actually the names of the files to be saved are 1.txt, 2.txt. 3.txt etc..there is error on saving 4.txt. You tell me how you want names of the files? There is problem on saving 4.txt file.
Could the filenames be as per the value in the fourth column? If c=1, let the file be 1.txt. If the c=100, let the file be 100.txt.
KSSV
KSSV on 28 Nov 2016
Edited: KSSV on 28 Nov 2016
The same things happens with the given code....did you open and see the created files?
I don't see any.
That's bad....you ask a question, some one answers it. And you have not seen you own work.
No.I meant I have been looking in all the MATLAB folders. Trying to figure where should it be. I didn't see any files created. If you check the screenshot you can see the input file 'data.txt' and 'KSSV.m' the code. But no output files were generated.
Your current directory is a directory that you do not have write access in.
How do I get write access for the current directory? I tried a few things but its still not working.
Good morning Mr. Roy. You open a new folder on desktop or something and run the code from that folder. Or you have to change the permissions as follows:
  • Right-click the file or folder for which you want to set permissions, click Properties, and then click the Security tab.
  • Click Edit to open the Permissions for Object dialog box.
  • Do what you want
And the text files the codes create will be named depending on what element it is going to have. In your case the first element to be written is 4, so it shall write in 4.txt. As there is no permission to write, so far no file is written. I apologize for my sharp comments.
Dr. Kolukula,
Thank you for trying to helping with the code. I had to Edit the permissions of the MATLAB folder in Program files. Now the code is generating files. But it needs to write relevant data from the input file. Please check the screenshot. This is the 4.txt created, it needs to have all the rows corresponding to 4 in the last column in data.txt. What do you think?
And you needn't apologise.
Regards,
Roy
clc; clear all ;
data = importdata('data.txt') ;
% last column
c = data(:,end) ;
% get repeated numbers
[rc,ia,ib] = unique(c) ;
for i = 1:length(rc) ;
idx = find(c==rc(i)) ;
k = [idx c(idx)] ;
fname = strcat(num2str(rc(i)),'.txt') ;
% save(fname,'k','-ascii') ;
dlmwrite(fname,k)
end
You should not be trying to store files in the MATLAB installation folders, you should be writing somewhere else like your Documents\MATLAB folder
Ironmaniac
Ironmaniac on 29 Nov 2016
Edited: Ironmaniac on 29 Nov 2016
Dr. K, Please check the screenshots. They are the outputs of the newer version of the code. The first three columns with precision as following is needed.
....................................................................
43217000 100.004 6.5286725301289810e+06 09
43217020 100.008 6.5286783341289805e+06 09
43217040 100.181 6.5286841901289811e+06 09
......................................................................
Mr. Roberson, I have the code and the input data in a folder on desktop. The output files are generated in the installation folder. What do I need to do?
Thanks to both of you, gentlemen.
-Roy
That's all....what you have to do is accept the answer.
Dr. K, the code needs some modification.
The first three columns with precision as following is needed.
....................................................................
43217000 100.004 6.5286725301289810e+06 09
43217020 100.008 6.5286783341289805e+06 09
43217040 100.181 6.5286841901289811e+06 09
......................................................................
Comment dlmwrite and use save given above it. Any ways it is not going to effect, if you intend do any calculation.
You want to include the first three columns too in the output?
clc; clear all ;
data = importdata('data.txt') ;
% last column
c = data(:,end) ;
% get repeated numbers
[rc,ia,ib] = unique(c) ;
for i = 1:length(rc) ;
idx = find(c==rc(i)) ;
k = [idx data(idx,:)] ;
fname = strcat(num2str(rc(i)),'.txt') ;
% save(fname,'k','-ascii') ;
dlmwrite(fname,k)
end
All the above was easy job..by this time you should have learned it. No more discussion. This question is closed.

Sign in to comment.

More Answers (0)

Categories

Find more on Functions in Help Center and File Exchange

Asked:

on 28 Nov 2016

Commented:

on 29 Nov 2016

Community Treasure Hunt

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

Start Hunting!