How to read specific part of the text file?

So, I have text file in Notepad data.txt which contain all information about some image for example
  • image1.gif
  • gif
  • 256
  • 256
  • 155.992455 321.222455 995.452774 741.123478
  • 15 24 35
  • 41 52 64
  • 58 59 50
so I have name of image, extension,height, width, matrix of gray image and matrix of image in this order in file. I made function which receives namefile and parameter which is needed to be read. So I want that the result of function be that parametar from text file for example function:
readformfile('data.txt','MatrixOfImage')
and result should be this
15 24 35
41 52 65
58 59 50
I have written the function but it reads all text from file and I don't know how to read only specific parameter from file
function result=readfromfile(namefile,parameter)
result=fileread(namefile);
end
I would be very grateful if someone could help me.

2 commentaires

dpb
dpb le 13 Déc 2014
I'd probably just build a routine that reads the whole file then sends back the section(s) requested. That way it's all one function to maintain and simply a choice of which data to return. Of course, if you think you're going to be looking at the same file over and over for various pieces, that's a lot of overhead so I'd try to rearrange my higher level usage to avoid that. Memory is cheap these days...so what if you've got some extra stuff hanging around waiting to be used later?
Well I made some routine that reads file by section. I just put for example # at the end of every section and then with function strread do this
[a,b,c,d,e,f]=strread(text,'%s %s %s %s %s %s','delimiter','#');
And then I have all section but how to get one I need?

Connectez-vous pour commenter.

 Réponse acceptée

per isakson
per isakson le 13 Déc 2014
Modifié(e) : per isakson le 13 Déc 2014
With Matlab there are many ways. The one below is one of them. Regard the first five lines as a "header" and use textscan.
>> num = cssm()
num =
15 24 35
41 52 64
58 59 50
where
function num = cssm()
fid = fopen( 'cssm.txt' );
cac = textscan(fid,'%f%f%f', 'Headerlines',5, 'CollectOutput',true );
fclose( fid );
num = cac{1};
end
and cssm.txt contains
image1.gif
gif
256
256
155.992455 321.222455 995.452774 741.123478
15 24 35
41 52 64
58 59 50
&nbsp
In response to comment
Try this modified function
>> out = cssm( 'cssm1.txt', 'name_of_image' )
out =
image1.gif
>> out = cssm( 'cssm2.txt', 'color_image' )
out =
78 44 28
77 32 71
55 12 53
>> out = cssm( 'cssm2.txt', 'RBG_image' )
name_of_image, extension, height, width, gray_image, color_image
out =
[]
>> out = cssm( 'cssm1.txt', 'width' )
out =
256
where
function out = cssm( filespec, param )
fid = fopen( filespec );
cac = textscan( fid, '%s', 'Delimiter','\n', 'CollectOutput',true );
fclose( fid );
switch lower( param )
case 'name_of_image'
out = cac{1,1};
case 'extension'
out = cac{2,1};
case 'height'
out = str2double( cac{3,1} );
case 'width'
out = str2double( cac{4,1} );
case 'gray_image'
out = str2num( cac{5,1} );
case 'color_image'
out = str2num( char(cac(6:8,1)) );
otherwise
str = 'name_of_image, extension, height, width, gray_image, color_image';
disp( str )
out = [];
end
end
and cssm2.txt contains
image2.gif
gif
128
128
456.972255 333.2147455 895.441274 125.111278
78 44 28
77 32 71
55 12 53

9 commentaires

Lolipop
Lolipop le 13 Déc 2014
Modifié(e) : Lolipop le 13 Déc 2014
This works..but I want when user use this function he can choose text file and parameter from text file and then that parameter write in commamd window.
So the name of text file and parameters (name of image, extension,height, width, matrix of gray image and matrix of image) are known only when user type them in.
For example user wants from file cssm.txt width of image and this function should give him width. Or user wants from another text file data.txt only extension from file.
I hope that you understand me what I'm trying to do...
per isakson
per isakson le 13 Déc 2014
What about "I made function which receives namefile and parameter which is needed to be read." ?
Sorry of my bad English..what I meant I made this
function result=readfromfile(namefile,parameter)
result=fileread(namefile);
end
I know what function receives,but I don't know how to write it?
per isakson
per isakson le 13 Déc 2014
No, I don't get it. I need more detailed descriptions of
  • parameter
  • result
Ok..I tried to do this task alone,but it doesn't work so I tried to explain you what I have done but I only confused you because of my bad English,so for a moment let's forget what I have done with this task. This program should display specific part of some text file. There are 4 text files.In each of 4 text files there are 6 parameters written in this order
Data1.txt
image1.gif
gif
256
256
155.992455 321.222455 995.452774 741.123478
15 24 35
41 52 64
58 59 50
so I have name of image, extension,height, width, matrix of gray image and matrix of image in this order in file. Of course in second file there is the same order of parametres only with different numbers.
Data2.txt
image2.gif
gif
128
128
456.972255 333.2147455 895.441274 125.111278
78 44 28
77 32 71
55 12 53
And so on.
So when user starts this function he can choose which text file and parameter from that text file he wants to display.
For example user should call function something like this
>> readfromfile('Data1.txt','width')
and result should be
>> 256
Hope you understand
If you have some question please ask me :)
Oh my God this is really working...THANK YOU SO MUCH... and please could you explain me this line
out = str2num( char(cac(6:8,1)) );
how you get whole matrix to display
per isakson
per isakson le 13 Déc 2014
Now I got it! I added a new function to the answer.
"explain me this line"
out = str2num( char(cac(6:8,1)) );
Run
cac = { '15 24 35'
'41 52 64'
'58 59 50' }
% convert the cell array of strings to a character array
str = char( cac )
% convert the character array to a double array
num = str2num( str )
it displays
cac =
'15 24 35'
'41 52 64'
'58 59 50'
str =
15 24 35
41 52 64
58 59 50
num =
15 24 35
41 52 64
58 59 50
and run
>> whos
Name Size Bytes Class Attributes
cac 3x1 384 cell
num 3x3 72 double
str 3x8 48 char
Lolipop
Lolipop le 13 Déc 2014
I got it! You really helped me a lot...Thank you for your time and if there is some another way to thank you,tell me :)

Connectez-vous pour commenter.

Plus de réponses (0)

Catégories

En savoir plus sur Data Import and Export dans Centre d'aide et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by