Info
This question is closed. Reopen it to edit or answer.
How do i search for a text and make it into a matrix, to display as an image? Using Structure functions.
1 view (last 30 days)
Show older comments
-Import the text file
-search for 'Samps/line:' & 'Number of lines:'
-Get the values(numbers) '512' & '243'.
-Convert the values to a 512 X 243 matrix
-Display it as a image.
1 Comment
Walter Roberson
on 31 Dec 2013
Why bother using a structure function? You have several steps leading up to displaying as an image, but you have not defined anything to be "returned" or anything in particular that you would insert into a structure.
Answers (3)
Image Analyst
on 27 Dec 2013
I'd use fopen(), then fgetl(), strfind() to look for the strings, sscanf() or str2double() to extract numbers out of strings (lines of text), finally fclose() to close the file. Then use imshow() to show the image. You're a smart engineer, you can do it - give it a try before asking us. Consider it a challenge. If you really can' t figure it out, even after looking at the examples in the help, come back with what code you do have and we'll get it working.
0 Comments
Walter Roberson
on 31 Dec 2013
textstr = fileread('text.txt');
perline = str2double( regexp(textstr, '(?:Samps/line:\s*)\d+', 'match') );
numlines = str2double( regexp(textstr, '(?:Number of lines:\s*)\d+',, 'match') );
nums = str2double( regexp( regexp(textstr, '(?:Height(nm)\n).*', 'match'), '\n', 'split') );
nums = reshape(nums, perline, numlines) .'; %assuming data is listed in row order in file
imagesc(nums);
outstruct = struct('perline', perline, 'numlines', numlines, 'cdata', nums);
6 Comments
Image Analyst
on 2 Jan 2014
I don't know - what are its values? Say, did you ever look at the debugging link I gave you? You should be able to do this simple kind of debugging, like setting breakpoints and checking variable values. Debugging via asking people takes hours and hours while doing it yourself is almost instant. You've already been at this task for 5 days.
Walter Roberson
on 2 Jan 2014
perline_string = regexp(textstr, '(?:Samps/line:\s*)\d+', 'match');
numlines_string = regexp(textstr, '(?:Number of lines:\s*)\d+', 'match');
perline = str2double(perline_string);
numlines = str2double(numlines_string);
if isnan(perline)
error( sprintf('Something went wrong calculating perline. The input string matched was |%s|\n', perline_string );
end
if isnan(numlines)
error( sprintf('Something went wrong calculating numlines. The input string matched was |%s|\n', numlines_string );
end
See Also
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!