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)
-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
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
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.

shane
shane on 27 Dec 2013
Edited: Walter Roberson on 30 Dec 2013
hi i'm trying to change this subfunction.
i want to use a structure function to replace the token method.
function [matrix] = Inputreader1(readbox,txtsearch)
%Set the text strings to search headers
txtsearch1 = '"\Samps/line:';
txtsearch2 = '"\Number of lines:';
%Set variables to signal string found to logical zero
foundStr1 = false;
foundStr2 = false;
foundStr3 = false;
%Create an empty matrix array
matrix = [];
%initialise default values
done_input = 0;
i = 1;
j = 1;
row1 = 0;
row2 = 0;
y = 0;
token = 0;
r=0;
k=0;
%%%~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~%%%
%open file
fid = fopen(readbox, 'r');
while (done_input == 0)
%read data from rows
readrowdata = fgets(fid);
k=1;
% fgets returns -1 if file is invalid
if (readrowdata == -1)
done_input = 1;
else
if (foundStr1 == false) || (foundStr2 == false) || (foundStr3 == false)
% search the first text from the row
searchResult1 = strfind(readrowdata, txtsearch1);
emptyResult1 = isempty(searchResult1);
%found first text
if emptyResult1 == false
%make sure row1 and row2 is empty
if (row1 == 0) || (row2 == 0)
%Find token in the string
[token, row1] = strtok(readrowdata);
%convertion of row1 into integer
row1 = sscanf(row1,'%d');
%inform that the first text is found
foundStr1 = true;
end
end
%%%~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~%%%
%condition to execute searching of second text
if (foundStr1 == true) && (foundStr2 == false)
%Search second text from row
searchResult2 = strfind(readrowdata,txtsearch2);
emptyResult2 = isempty(searchResult2);
if emptyResult2 == false
if (row2 == 0)
[token, remain] = strtok(readrowdata);
[r,remain] = strtok(remain);
[r,remain] = strtok(remain);
[row2] = strtok(remain,'"');
%convertion of row2 into integer
row2 = sscanf(row2,'%i');
%signal that second text is found
foundStr2 = true;
end
end
end
%%%*****************************************************************%%%
%Condition to execute searching of Height(nm)
if (foundStr1 == true) && (foundStr2 == true) && (foundStr3 == false)
%search the text from the row
searchResult3 = strfind(readrowdata, txtsearch);
emptyResult3 = isempty(searchResult3);
%Signal that text is found
if emptyResult3 == false
foundStr3 = true;
k=0;
end
end
end
if (foundStr3 == true)&& (k==1) && (done_input==0)
if i<=row2
if j<=row1
matrix(i, j) = sscanf(readrowdata, '%f');
else
i=i+1;
j=1;
matrix(i, j) = sscanf(readrowdata, '%f');
end
j=j+1;
end
end
end
end
end
  6 Comments

Walter Roberson
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
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
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

Community Treasure Hunt

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

Start Hunting!