I am trying to use imshow to take data in a .txt file and print out the image made from the data.
You are now following this question
- You will see updates in your followed content feed.
- You may receive emails, depending on your communication preferences.
An Error Occurred
Unable to complete the action because of changes made to the page. Reload the page to see its updated state.
Show older comments
So far my code looks like this
a = dlmread('data.txt');
imshow(a);
But when I go to run the code I get an error saying "Error using abs too many input arguments." The .txt file has a ton of data points inside it and I made sure to save the .txt file in the same folder as my code so I am not sure why it is not able to read my data.
Accepted Answer
I'm going to guess that this is the same homework assignment. If not, similar operations should apply. The example only shows how to rectify the image from the vectorized positional and intensity information. You'll have to figure out how to read the data from the file into the corresponding vectors and deal with the output scaling/casting as mentioned.
10 Comments
ssmith
on 26 Oct 2021
Hello, not exactly. I know how to refer to data from an excel file however, when I go to do the same thing for .txt file I get an error message provided above. I do not have an image like in the example coin you provided the link to. I only have data that is supposed to produce an image.
DGM
on 26 Oct 2021
I'm only working off of a loose description of the file itself as a point list. It's not clear to what the error refers, as you didn't include the entire error message. Even if 'a' correctly represents the text file, imshow() does not handle data in such a format. Images need to be presented as mxn (or mxnx3) arrays.
The example I gave shows how the vectorized data is converted into such an array. I added a simple example of how the file can be read in a manner which can be used with the rectifying example. I'm still making assumptions about how exactly the file is formatted and what the image data is.
If the file contains no header, then I'm going to assume that it's being read correctly to the array 'a'.
I adjusted the example accordingly. All you should need to do is extract the x, z and intensity vectors and then rectify the image.
a = dlmread('testfile.txt');
x = a(:,1);
z = a(:,3);
A = mat2gray(a(:,4)); % normalize
% rectify
imheight = numel(unique(z));
imwidth = numel(unique(x));
B = reshape(A,imwidth,imheight).';
% display
imshow(B)
ssmith
on 26 Oct 2021
This just prints out the data points taken from the .txt file to be displayed in the command window, it does not print out the image even though I have it display imshow after and then write end to stop to code. Instead it tries to read the millions of data points I have. Do you know where I went wrong?
The above example shouldn't print anything to console. It displays the image B. If you want to save it, use imwrite().
Attached is a a small example file of similar format:
+1.1000000E+00, +0.0000000E+00, +1.1000000E+00, +157
+1.2000000E+00, +0.0000000E+00, +1.1000000E+00, +157
+1.3000000E+00, +0.0000000E+00, +1.1000000E+00, +157
+1.4000000E+00, +0.0000000E+00, +1.1000000E+00, +158
% ...
Which can be read as described.
a = dlmread('cmanvec.txt');
x = a(:,1);
z = a(:,3);
A = mat2gray(a(:,4)); % normalize
% rectify
imheight = numel(unique(z));
imwidth = numel(unique(x));
B = reshape(A,imwidth,imheight).';
% display
imshow(B)

If you're doing something else, you'll have to show what you're actually doing
ssmith
on 27 Oct 2021
Yes, that example appears to be close to what I am doing, however, how did you get your image to print? When I run the code with my .txt file the command window will only print out the data points in an array; no image/figure pops up or is made.
The last comment was run entirely in-browser. What you see is exactly how I ran it. There shouldn't be anything that can cause anything to be dumped to console. All the lines are terminated with ";" to suppress output. If you missed a terminating semicolon, it would definitely take a while to dump 5.3 million lines to console.
Before trying to wrangle the whole data file, can you replicate the above behavior with the attached test file cmanvec.txt? Does that one display correctly?
If cmanvec.txt does not display, but dumps a bunch of garbage to console, then there is possibly a file conflicting with imshow() or something else.
If cmanvec.txt displays fine, but your image does not, then there's something else going on. If there is some unforeseen difference between my test file and yours (other than the sheer size), it might simply be easiest to zip the text file and upload it somewhere off-site. The attachments here on the forum only allow 5MB, but I'm guessing that your text file should be somewhere around 15MB once compressed. We can cross that bridge if necessary.
ssmith
on 27 Oct 2021
did you write an additional function of mat2gray? My code says it cannot be found when using which mat2gray -all in the command window.
I don't know why you'd get an error about a function/variable called "first". There isn't anything in the code called "first".
If you don't have mat2gray(), I'm guessing you don't have Image Processing Toolbox. You can do this instead for that line:
%A = mat2gray(a(:,4)); % normalize (needs IPT)
A = normalize(a(:,4),'range'); % normalize without IPT
That worked! Thank you so much for your help!!!
More Answers (0)
Categories
Find more on Convert Image Type in Help Center and File Exchange
Tags
See Also
Select a Web Site
Choose a web site to get translated content where available and see local events and offers. Based on your location, we recommend that you select: .
You can also select a web site from the following list
How to Get Best Site Performance
Select the China site (in Chinese or English) for best site performance. Other MathWorks country sites are not optimized for visits from your location.
Americas
- América Latina (Español)
- Canada (English)
- United States (English)
Europe
- Belgium (English)
- Denmark (English)
- Deutschland (Deutsch)
- España (Español)
- Finland (English)
- France (Français)
- Ireland (English)
- Italia (Italiano)
- Luxembourg (English)
- Netherlands (English)
- Norway (English)
- Österreich (Deutsch)
- Portugal (English)
- Sweden (English)
- Switzerland
- United Kingdom (English)