How do i search for a text and make it into a matrix, to display as an image? Using Structure functions.
Informazioni
Questa domanda è chiusa. Riaprila per modificarla o per rispondere.
Mostra commenti meno recenti
-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 Commento
Walter Roberson
il 31 Dic 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.
Risposte (3)
Image Analyst
il 27 Dic 2013
0 voti
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.
shane
il 27 Dic 2013
Modificato: Walter Roberson
il 30 Dic 2013
6 Commenti
Image Analyst
il 27 Dic 2013
You posted this as an official "Answer" so it sounds like this is the code you got working, so that's good. Congratulations.
shane
il 30 Dic 2013
Image Analyst
il 30 Dic 2013
There is a function called struct(). Look it up in the help.
shane
il 31 Dic 2013
shane
il 31 Dic 2013
Image Analyst
il 31 Dic 2013
Walter Roberson
il 31 Dic 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 Commenti
shane
il 2 Gen 2014
Image Analyst
il 2 Gen 2014
Find out which one (perline, or numlines) is zero, negative, or has a fractional part, then fix it.
shane
il 2 Gen 2014
shane
il 2 Gen 2014
Image Analyst
il 2 Gen 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
il 2 Gen 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
Questa domanda è chiusa.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!